对ArrayList中的对象进行排序? [英] Sorting objects, that are in an ArrayList?

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

问题描述

所以我有一个ArrayList,用以下对象填充:


myAL.Add(new CustomObject(parm1,parm2));


我正在从ObjectDataSource中使用这个ArrayList,并希望这个支持排序

(因为它最终会在GridView中使用)。

我不能简单地对ArrayList进行排序(因为它只知道它持有一堆

对象)。所以我需要一种基于

数据对ArrayList进行排序的方法 - 它在已加载的对象中。


似乎必须是一些相对简单的方法,我可以使

CustomObject()类可排序...任何想法??? TIA

So I have an ArrayList that gets populated with objects like:

myAL.Add(new CustomObject(parm1,parm2));

I''m consuming this ArrayList from an ObjectDataSource and would like to have
this support sorting (because it''s ultimately being consumed in a GridView).
I can''t simply sort the ArrayList (because it only knows it''s holding a
bunch of objects). So I need a way to sort the ArrayList, based on the
data - that is within the objects that it has loaded.

It seems like there must be some relatively simple way that I can make that
CustomObject() class sortable... any ideas??? TIA

推荐答案

>似乎必须有一些相对简单的方法,我可以使
> It seems like there must be some relatively simple way that I can make
那个CustomObject()类可以排序......任何想法??? TIA
that CustomObject() class sortable... any ideas??? TIA




您只需为默认排序顺序实现IComparable或创建一个

外部[或内部]类,为任何其他实现IComparer

[非默认]排序顺序。

Wiktor Zychla



you just implement IComparable for the default sort order or create an
external [or inner] class that implements IComparer for any other
[non-default] sort order.

Wiktor Zychla





我希望我以正确的方式解释你的问题。


你需要使用IComparable接口。 - > YourClass:IComparable

在你必须实现的''CompareTo(对象x)'方法中,你可以比较你想要的任何东西,并返回一个小于零的数字你

表示''This Instance''小于x,返回0你

表示''This Instance''等于x和更大的数字比
零表示''此实例''大于x。

但你是(可以)定义标准,在这种情况下你返回-1,0 < br $>
或1


最后你可以调用myArrayList.Sort(...);


见:
http://msdn.microsoft.com/library/de...classtopic.asp
http://msdn.microsoft.com/library/de...aretotopic.asp


希望这有助于

Roland

Hi

I hope I understud your question in a correct way.

You need to use the IComparable Interface. -> YourClass : IComparable
In the method ''CompareTo(object x)'' which you have to implement you can
compare anything you want and by returning a number less than zero you
indicate that ''This Instance'' is smaller than x, by return 0 you
indicate that ''This Instance'' is equal to x and a number bigger than
zero indicates that ''This Instance'' is bigger than x.
But you are (can) defining the criteria in which case you return -1, 0
or 1

Finally you can call myArrayList.Sort(...);

see:
http://msdn.microsoft.com/library/de...classtopic.asp
http://msdn.microsoft.com/library/de...aretotopic.asp

Hope this helps
Roland


RCS< rs **** @ gmail.com>写道:
RCS <rs****@gmail.com> wrote:
所以我有一个ArrayList,用以下对象填充:

myAL.Add(new CustomObject(parm1,parm2));
我正在从ObjectDataSource中使用这个ArrayList,并希望得到这种支持排序(因为它最终会在GridView中使用)。
我不能简单地对ArrayList进行排序(因为它只知道它有一堆
对象)。所以我需要一种基于
数据对ArrayList进行排序的方法 - 它在已经加载的对象中。

似乎必须有一些相对简单的方法,我可以使那个
CustomObject()类可排序......任何想法??? TIA
So I have an ArrayList that gets populated with objects like:

myAL.Add(new CustomObject(parm1,parm2));

I''m consuming this ArrayList from an ObjectDataSource and would like to have
this support sorting (because it''s ultimately being consumed in a GridView).
I can''t simply sort the ArrayList (because it only knows it''s holding a
bunch of objects). So I need a way to sort the ArrayList, based on the
data - that is within the objects that it has loaded.

It seems like there must be some relatively simple way that I can make that
CustomObject() class sortable... any ideas??? TIA




您可以通过在类本身内实现IComparable或者通过传递可以比较两个的IComparer来对ArrayList进行排序。 />
CustomObjects调用ArrayList.Sort。


-

Jon Skeet - < sk *** @ pobox.com> ;
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件



You can sort the ArrayList either by implementing IComparable within
the class itself, or by passing an IComparer which can compare two
CustomObjects into the call to ArrayList.Sort.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


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

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