I’m trying to get specific elements of a listOf structure with the function findAll plus a lambda function, and then sort that result and keep that sorting of those elements saved in the list. The structure of the listOf has an id and an age, so what I want is get all the items with id=0 and then sort the ages of that result, keeping that modification in the variable.
This is what I’ve tried, but it doesn’t work
list.FindAll(Function(p1) p1.id = 0).Sort(Function(p1, p2) p1.age > p2.age)
Your line of code doesn’t work, because FindAll returns a new list with the items found, and you don’t assign that new list to the list variable. Also, you can’t chain the call to Sort because this function changes the underlying list and doesn’t return anything. To use FindAll and Sort together you will need two statements:
It would be easier to use Linq to do this, as you can always chain expressions with Linq, and the sorting is easier to do. But you need to assign the result to the list again: