根据对象的属性对对象列表进行排序

编程入门 行业动态 更新时间:2024-10-27 02:20:56
本文介绍了根据对象的属性对对象列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

根据对象的属性对对象列表进行排序

本文发布于:2023-11-30 05:48:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   属性   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!