Sorting a list of ints and then sorting a bunch of other non-int lists to the same reorder [Solved]

How would I go about sorting a list of ints in numerical order and then sorting a bunch of other non-int lists to the same order the list of ints has been sorted to?

I’ve tried all kinds of ideas but nothing has worked out so far. This would be really simple if it was possible to sort a dictionary but from what I’ve read that is impossible, so I’m stuck using a bunch of lists instead but can’t find a way to sort one and have the others follow the sort order of the first one.

Just to clarify what I mean, lets say I have a list of ints that are 4, 7, 3, 9 in indexes 0, 1, 2 and 3. I also have a list of strings, “orange”, “apple”, “banana”, “grape” in indexes 0, 1, 2 and 3. So if the lists were beside each other visually, 4 is orange, 7 is apple, 3 is banana, and 9 is grape. I then sort the int list and get 9, 7, 4, 3. How do I then have the string list rearrange itself in the exact same way to be grape, apple, orange, banana?

Maybe:

or:
https://msdn.microsoft.com/en-us/library/85y6y2d3(v=vs.110).aspx
(or: Array.Sort Method (System) | Microsoft Learn)

I would also consider consolidating your multiple lists into one list using a data container object, something like:

public class FruitData
{
    public int id;
    public string name;
}

Then sort that list by id or whatever element you need.

Thanks for the replies guys. I got it working nicely with GroZZleRs suggestion.

1 Like