Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How would I sort a list of objects?
For example,

C#
List<someobject> list = new List<someobject>();
list.add(someObject) 100 times

Now there is
C#
someObject.someDoubleValue = .232;


Each someObject has a someDoubleValue property which holds a double value.

Now, I want to sort the list based on someObject.someDoubleValues so the list would be

C#
list[0] = someobject1.someDoubleValue =.2
list[1] = someObject2.someDoubleValue =.3
list[2] = someObject3.someDoubleValue = .4 

etc in the list
Posted
Updated 21-Jun-12 18:05pm
v2

Each someObject is an instance of some class. Implement the IComparable interface[^] for that class.

Without this, list.Sort() cannot know what order you want the list to be sorted in.
 
Share this answer
 
v2
Comments
Maciej Los 22-Jun-12 3:21am    
Good answer, my 5!
lukeer 22-Jun-12 3:45am    
Thanks.
C#
list.sort();

you will get 0.2,0.3,04
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900