如何对对象列表进行排序? 例如,
How would I sort a list of objects? For example,
List<someobject> list = new List<someobject>(); list.add(someObject) 100 times现在有
Now there is
someObject.someDoubleValue = .232;每个someObject都有一个doubleDoubleValue属性,该属性包含一个双精度值. 现在,我想基于someObject.someDoubleValues对列表进行排序,以便该列表为
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
list[0] = someobject1.someDoubleValue =.2 list[1] = someObject2.someDoubleValue =.3 list[2] = someObject3.someDoubleValue = .4推荐答案
每个someObject是某个类的实例.实现 IComparable接口 [ ^ ]对该类. 没有这个,list.Sort()就无法知道您要对列表进行排序的顺序. 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.
list.sort();
您将获得0.2,0.3,04
you will get 0.2,0.3,04