Skip to main content
gamedev.net
🔒 Locked

[.net] ArrayList.Sort() Function

Started by rob64464 2 replies 1.3k views
Original Post
rob64464
rob64464
Hello, I have been browsing the internet for information on the ArrayList.Sort(); function. I am making Leader boards for a game and when i call ArrayList.Sort(); it does this 9998 Rob 94 Rob 8778 Mark 88 James Is there a method for an arraylist to sort by biggest values first?
The Frugal Gourmet
The Frugal Gourmet
With Sort(), you are allowed to specify your own comparison class (it must be of type IComparer). So, if you would like to compare something in custom fashion just make an IComparer class (or use an existing one) that contains its own Compare() method that will compare two objects however you like.
Co-creator of Star Bandits -- a graphical Science Fiction multiplayer online game, in the style of "Trade Wars'.
thona
thona
Quote:
Is there a method for an arraylist to sort by biggest values first?


It DOES sort by the biggest value first, dude.

String sorting does not care what your string contains, semantically. It is sorting char by char bachar.

9998 Rob
is larger than
94 Rob

in the second char. That is it.

If you want sensible sorting then:

* Either padd the values with 0 or, better,
* store objects (classes) containing the numeric value as integer or something, and the string and then implement a comparer that only compares the numeric values.

Right now it does exactly what you ask it to do. Not a syntactical, but a semantical error - you do not know what you want, from the computer's point of view.
RegardsThomas TomiczekTHONA Consulting Ltd.(Microsoft MVP C#/.NET)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.