根据对象的属性对对象列表进行排序 [英] sorting a list of objects based on its properties

查看:69
本文介绍了根据对象的属性对对象列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对对象列表进行排序?
例如,

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接口 [
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


这篇关于根据对象的属性对对象列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆