如何通过对象属性之一对对象的ArrayList进行排序? [英] How do I order an ArrayList of objects by one of the objects attributes?

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

问题描述

如何通过对象属性之一对对象的ArrayList进行排序?

How do I order an ArrayList of objects by one of the objects attributes?

也请不要建议任何与List<T>有关的技术.对于我当前使用的软件,不可能使用List<T>.

And please do not suggest any technique to do with List<T>. For my current software, using List<T> is out of the question.

推荐答案

您需要为您的实体实现IComparer,例如:

You need to implement IComparer for your entity like:

public class MyClassComparer : IComparer<MyClass>
{
    public int Compare(MyClass _x, MyClass _y)
    {
        return _x.MyProp.CompareTo(_y.MyProp);
    }
}

并将其传递给 ArrayList.Sort 喜欢:

myArrayList.Sort(new MyClassComparer());

这篇关于如何通过对象属性之一对对象的ArrayList进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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