Sorting an Arraylist filled with structs.

So, I have a struct like this:

public static struct Score { public string Name; public int HighScore; }

and i place a number of these in an arraylist.

I know there is a sort method attahced to the arraylist, but how do I use it?

I would like to sort the arraylist based on the highscore, but I can't seem to figure out how to do it in an easy way.

Any hints would be great!

it would be nice to be able to do something like this:


Thanks. :)


Something like this should do it:

public class StructComparer : IComparer
    public int CompareStructs(object x, object y)
        if (!(x is YourStruct) || !(y is YourStruct)) return 0;
        YourStruct a = (YourStruct)x;
        YourStruct b = (YourStruct)y;

        return a.HighScore.CompareTo(b.HighScore);

//when you want to sort...
yourArrayList.Sort(new StructComparer());

I don't think you can sort structs. For sorting to work in C# you have to implement the IComparer interface, which basically is done by overriding the Compare function. As structs can't have methods you'll have to use a class for that.