排序对象的Arraylist [英] Sorting Arraylist Of Objects

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

问题描述

我想在多个属性上对对象的Arraylist进行排序。对于

实例,我有一个Sort Index属性和一个ID属性(都是整数)。

所以,我的排序结果如下:


排序索引,id

1000,1

1000,2

1000,3

1001,1

1001,2

....


我知道我可以实现IComparable.CompareTo到在一个属性上排序,但是我不知道如何对两个属性进行排序。感谢您的帮助。

解决方案

这样做的好方法如下:


在Compare方法中,为每个对象创建一个字符串,该字符串具有要排序的属性的

连接字符串表示形式,

然后比较字符串。


例如,假设您的示例中的第一个属性具有最大值

长度,字符串,五个字符,第二个属性具有最大值

长度,一个字符串,四个字符。所以你在你的例子中给出的第一个属性可以是0到99999之间的值,而第二个

可以有从0到9999的值。然后你'' d做这样的事情:


dim str1 as string = firstObj.Property1.ToString()。Padleft(5," 0" c)+

firstObj.Property2.ToString()。PadLeft(4," 0" c)

dim str2 as string = secondObj.Property1.ToString()。Padleft(5," 0" c)+

secondObj.Property2.ToString()。PadLeft(4," 0" c)


返回String.Compare(str1,str2)


您将比较像010000001这样的字符串。和010010002。


要对这两个属性进行降序排序,你会:


return - (String.Compare(str1) ,str2)

并且只对一个属性进行降序排序,从它可以容纳的最高值中减去该属性值

并执行ToString和Padleft这是

的价值。所以下面的第一个例子,在物业上升一下

并在物业二下降,看起来像是:010009998。


HTH,

Tom Dacon

Dacon Software Consulting

" Kittyhawk" Ki *** ****@discussions.microsoft.com写信息

新闻:82 *************************** ******* @ microsof t.com ...


>我想在多个属性上对对象的Arraylist进行排序。对于

实例,我有一个Sort Index属性和一个ID属性(都是整数)。

所以,我的排序结果会看起来像这样:


排序索引,id

1000,1

1000,2

1000 ,3

1001,1

1001,2

...


我知道我可以实现IComparable.CompareTo对一个属性进行排序,但是我不知道如何对两个属性进行排序。感谢您的帮助。



在您的比较例程中,您应该能够比较两个属性以查看

哪个类实例应该是第一个。

-

休斯顿的丹尼斯

Kittyhawk写道:


我想在多个属性上对对象的Arraylist进行排序。对于

实例,我有一个Sort Index属性和一个ID属性(都是整数)。

所以,我的排序结果如下:


排序索引,id

1000,1

1000,2

1000,3

1001,1

1001,2

...


我知道我可以实现IComparable.CompareTo来排序在一个属性,但我

不完全清楚如何排序两个。感谢您的帮助。


Tom Dacon写道:


这是一个很好的方法如下:


在Compare方法中,为每个对象创建一个字符串,该字符串具有要排序的属性的

串联字符串表示形式,

然后比较字符串。


例如,假设您示例中的第一个属性具有最大值

长度,如一个字符串,由五个字符组成,第二个字符串的长度最大为

,字符串为四个字符。所以你在你的例子中给出的第一个属性可以是0到99999之间的值,而第二个

可以有从0到9999的值。然后你'' d做这样的事情:


dim str1 as string = firstObj.Property1.ToString()。Padleft(5," 0" c)+

firstObj.Property2.ToString()。PadLeft(4," 0" c)

dim str2 as string = secondObj.Property1.ToString()。Padleft(5," 0" c)+

secondObj.Property2.ToString()。PadLeft(4," 0" c)


返回String.Compare(str1,str2)


您将比较像010000001这样的字符串。和010010002。


要对这两个属性进行降序排序,你会:


return - (String.Compare(str1) ,str2)

并且只对一个属性进行降序排序,从它可以容纳的最高值中减去该属性值

并执行ToString和Padleft这是

的价值。所以下面的第一个例子,在物业上升一下

并在物业二下降,看起来像是:010009998。


HTH,

Tom Dacon

Dacon Software Consulting


" Kittyhawk" Ki*******@discussions.microsoft.com写信息

新闻:82 *********************** *********** @ microsof t.com ...


我想在多个属性上对对象的Arraylist进行排序。对于

实例,我有一个Sort Index属性和一个ID属性(都是整数)。

所以,我的排序的结果将是l喜欢这样:


排序索引,id

1000,1

1000,2

1000,3

1001,1

1001,2

...


我知道我可以实现IComparable.CompareTo来对一个属性进行排序,但是我不知道如何对两个属性进行排序。谢谢你的帮助。



为什么在没有必要时创建字符串?他需要做的就是在CompareTo方法中首先检查索引的值。如果他们

不相等,那么就对索引进行比较。如果索引是
等于,那么对ID进行比较:


如果索引不相等,那么Id的值将赢得''无论如何都会影响

排序。


如果obj1.Index< obj2.Index那么

返回obj1.Index.CompareTo (Obj2.Index)

否则

返回obj1.Id.CompareTo(obj2.Id)

结束如果


I would like to sort an Arraylist of objects on multiple properties. For
instance, I have a Sort Index property and an ID property (both integers).
So, the results of my sort would look like this:

sort index, id
1000,1
1000,2
1000,3
1001,1
1001,2
....

I know I can implement IComparable.CompareTo to sort on one property but I
am not completely clear on how to sort on two. Thanks for any help.

解决方案

A good way to do this is as follows:

In the Compare method, for each object create a string that has the
concatenated string representations of the properties you want to sort on,
and then compare the strings.

For instance, suppose the first property in your example has a maximum
length, as a string, of five characters, and the second one has a maximum
length, as a string, of four characters. So the first property which you
give in your example could have values from zero to 99999, and the second
one could have values from zero to 9999. Then you''d do something like this:

dim str1 as string = firstObj.Property1.ToString().Padleft(5, "0"c) +
firstObj.Property2.ToString().PadLeft(4, "0"c)
dim str2 as string = secondObj.Property1.ToString().Padleft(5, "0"c) +
secondObj.Property2.ToString().PadLeft(4, "0"c)

return String.Compare(str1, str2)

You''d be comparing strings like "010000001" and "010010002".

To sort descending on both properties, you''d:

return -(String.Compare(str1, str2)

And to sort descending on just one property, subtract that property value
from the highest value it can hold and do the ToString and Padleft on that
value. So your first example line below, to sort ascending on property one
and descending on property two, would look like: "010009998".

HTH,
Tom Dacon
Dacon Software Consulting
"Kittyhawk" <Ki*******@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...

>I would like to sort an Arraylist of objects on multiple properties. For
instance, I have a Sort Index property and an ID property (both integers).
So, the results of my sort would look like this:

sort index, id
1000,1
1000,2
1000,3
1001,1
1001,2
...

I know I can implement IComparable.CompareTo to sort on one property but I
am not completely clear on how to sort on two. Thanks for any help.



In your compare routine, you should be able to compare both properties to see
which class instance should be first.
--
Dennis in Houston
"Kittyhawk" wrote:

I would like to sort an Arraylist of objects on multiple properties. For
instance, I have a Sort Index property and an ID property (both integers).
So, the results of my sort would look like this:

sort index, id
1000,1
1000,2
1000,3
1001,1
1001,2
...

I know I can implement IComparable.CompareTo to sort on one property but I
am not completely clear on how to sort on two. Thanks for any help.


Tom Dacon wrote:

A good way to do this is as follows:

In the Compare method, for each object create a string that has the
concatenated string representations of the properties you want to sort on,
and then compare the strings.

For instance, suppose the first property in your example has a maximum
length, as a string, of five characters, and the second one has a maximum
length, as a string, of four characters. So the first property which you
give in your example could have values from zero to 99999, and the second
one could have values from zero to 9999. Then you''d do something like this:

dim str1 as string = firstObj.Property1.ToString().Padleft(5, "0"c) +
firstObj.Property2.ToString().PadLeft(4, "0"c)
dim str2 as string = secondObj.Property1.ToString().Padleft(5, "0"c) +
secondObj.Property2.ToString().PadLeft(4, "0"c)

return String.Compare(str1, str2)

You''d be comparing strings like "010000001" and "010010002".

To sort descending on both properties, you''d:

return -(String.Compare(str1, str2)

And to sort descending on just one property, subtract that property value
from the highest value it can hold and do the ToString and Padleft on that
value. So your first example line below, to sort ascending on property one
and descending on property two, would look like: "010009998".

HTH,
Tom Dacon
Dacon Software Consulting
"Kittyhawk" <Ki*******@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...

I would like to sort an Arraylist of objects on multiple properties. For
instance, I have a Sort Index property and an ID property (both integers).
So, the results of my sort would look like this:

sort index, id
1000,1
1000,2
1000,3
1001,1
1001,2
...

I know I can implement IComparable.CompareTo to sort on one property but I
am not completely clear on how to sort on two. Thanks for any help.

Why create strings when it is not necessary? All he needs to do is in
the CompareTo method to first check the value of the index. If they
are not equal then do the compare on the index. If the indexes are
equal, then do the compare on the ID:

If the indexes are not equal, then the value of the Id won''t affect the
sort anyway.

If obj1.Index <obj2.Index Then
return obj1.Index.CompareTo(Obj2.Index)
Else
return obj1.Id.CompareTo(obj2.Id)
End If


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

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