i have a List of objects and i want to sort by a certain field and then find out what “rank” or index is a certain name.
for example, lets say i have a:
List<Location> Locations= new List<Location>();
and i want to sort by Popularity
var list = this.Locations.OrderBy(r => r.PopularityPct);
i now want to find out what is the index of “Spain” (NOTE: “Spain” would be a lookup of the Name property, where Name would be a property of the location object) now that this list is sorted by popularity.
what is the easiest way of doing this?
You can easily get all of the names and indexes like this:
Then, for example:
Or print everything:
This is assuming you want the post-sorted rank. If you want the index in the initial list, you’d switch the order: