ArrayList.Sort问题 [英] ArrayList.Sort Question

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

问题描述

你好,我一直在玩我的arraylist并且有一些

麻烦。也许只是错了。我的arraylist包含对象

,对象的一个​​成员是''name。''我想基于object.name排序

arraylist - 是的可能吗?

谢谢!

Derek

Hi there, I have been playing with sorting my arraylist and having some
troubles. Maybe just going about it wrong. My arraylist contains objects
and one of the members of the object is ''name.'' I would like to sort the
arraylist based on object.name - is that possible?
Thanks!
Derek

推荐答案

您需要实现IComparable接口在类声明中

你在ArrayList中拥有的对象。


这是一篇文章,展示了如何:

http://www.devx.com/dotnet/Article/21089

-

Mike


Mike McIntyre

Visual Basic MVP www.getdotnetcode.com


"德里克·马丁 < DM ***** @ DONTSPAMMEokstateDOT.edu>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
You will need to implement the IComparable interface in the class statement
of the object(s) you have in the ArrayList.

Here is an article that shows how:

http://www.devx.com/dotnet/Article/21089
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
你好,我一直在玩我的arraylist排序和一些麻烦。也许只是错了。我的arraylist包含对象
,对象的一个​​成员是''name。''我想根据object.name排序
arraylist - 这可能吗?
谢谢!
Derek
Hi there, I have been playing with sorting my arraylist and having some
troubles. Maybe just going about it wrong. My arraylist contains objects
and one of the members of the object is ''name.'' I would like to sort the
arraylist based on object.name - is that possible?
Thanks!
Derek



您必须拥有自己的比较器来实现IComparer

接口。以下是一个例子:


公共类TestClass

私有mName为字符串


公共属性名称( )作为字符串

获取

返回mName

结束获取

设置(ByVal值为字符串)

mName =价值

结束套件

结束物业

结束等级


公共类TestClassSort

实现IComparer


公共函数比较(ByVal x As Object,ByVal y As Object)As Integer

Implements System.Collections.IComparer.Compare

返回String.Compare(DirectCast(x,TestClass).Name(),DirectCast(y,

TestClass).Name(), CompareMethod.Binary)

结束功能

结束课程


私人Sub TestSort()

昏暗arr As New ArrayList

Dim o(2)As TestClass

o(0)= New TestClass

o(0).Name =" One" ;

o(1)= New TestClass

o(1).Name =" Two"

o(2)= New TestClass

o(2).Name =" Three"

arr.AddRange(o)

for i As Integer = 0 to arr.Count - 1

Console.WriteLine(DirectCast(arr.Item(i),TestClass).Name)

下一页

arr.Sort(New TestClassSort)

for i As Integer = 0 to arr.Count - 1

Console.WriteLine(DirectCast(arr.Item(i) ,TestClass).Name)

下一页

End Sub

希望有所帮助..

Imran。

" Derek Martin" < DM ***** @ DONTSPAMMEokstateDOT.edu>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
You''ll have to have your own comparer that implements the IComparer
interface. Here''s an example:

Public Class TestClass
Private mName As String

Public Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
End Set
End Property
End Class

Public Class TestClassSort
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare
Return String.Compare(DirectCast(x, TestClass).Name(), DirectCast(y,
TestClass).Name(), CompareMethod.Binary)
End Function
End Class

Private Sub TestSort()
Dim arr As New ArrayList
Dim o(2) As TestClass
o(0) = New TestClass
o(0).Name = "One"
o(1) = New TestClass
o(1).Name = "Two"
o(2) = New TestClass
o(2).Name = "Three"
arr.AddRange(o)
For i As Integer = 0 To arr.Count - 1
Console.WriteLine(DirectCast(arr.Item(i), TestClass).Name)
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLine(DirectCast(arr.Item(i), TestClass).Name)
Next
End Sub
hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
你好,我一直在玩我的arraylist排序和一些麻烦。也许只是错了。我的arraylist包含对象
,对象的一个​​成员是''name。''我想根据object.name排序
arraylist - 这可能吗?
谢谢!
Derek
Hi there, I have been playing with sorting my arraylist and having some
troubles. Maybe just going about it wrong. My arraylist contains objects
and one of the members of the object is ''name.'' I would like to sort the
arraylist based on object.name - is that possible?
Thanks!
Derek



谢谢两位!我将调查:-)


Derek


" Imran Koradia" <无**** @ microsoft.com>在消息中写道

news:u9 ************** @ tk2msftngp13.phx.gbl ...
Thank you both! I will investigate :-)

Derek

"Imran Koradia" <no****@microsoft.com> wrote in message
news:u9**************@tk2msftngp13.phx.gbl...
你将拥有拥有自己的比较器来实现IComparer
接口。以下是一个例子:

公共类TestClass
私有mName为字符串

公共属性名称()为字符串
获取
返回mName
结束获取
设置(ByVal值为字符串)
mName =值
结束集
结束属性
结束类
实现IComparer

公共函数比较(ByVal x As Object,ByVal y As Object)As
Integer
实现System.Collections.IComparer。比较
返回String.Compare(DirectCast(x,TestClass).Name(),
DirectCast(y,
TestClass).Name(),CompareMethod.Binary)
结束函数
结束类

Private Sub TestSort()
Dim arr As New ArrayList
Dim o(2)As TestClass
o(0)= New TestClass
o(0).Name =" One"
o(1)= New TestClass
o(1).Name =" Two"
o(2)=新TestClass
o(2).Name =" Three"
arr.AddRange(o)
对于i as Integer = 0 to arr.Count - 1
Console.WriteLine(DirectCast(arr.Item) (i),TestClass)。Name)
下一页
arr.Sort(New TestClassSort)
对于i as Integer = 0 to arr.Count - 1
Console.WriteLine(DirectCast) (arr.Item(i),TestClass).Name)
下一页
结束子

希望有所帮助..
Imran。

Derek Martin < DM ***** @ DONTSPAMMEokstateDOT.edu>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
You''ll have to have your own comparer that implements the IComparer
interface. Here''s an example:

Public Class TestClass
Private mName As String

Public Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
End Set
End Property
End Class

Public Class TestClassSort
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer
Implements System.Collections.IComparer.Compare
Return String.Compare(DirectCast(x, TestClass).Name(),
DirectCast(y,
TestClass).Name(), CompareMethod.Binary)
End Function
End Class

Private Sub TestSort()
Dim arr As New ArrayList
Dim o(2) As TestClass
o(0) = New TestClass
o(0).Name = "One"
o(1) = New TestClass
o(1).Name = "Two"
o(2) = New TestClass
o(2).Name = "Three"
arr.AddRange(o)
For i As Integer = 0 To arr.Count - 1
Console.WriteLine(DirectCast(arr.Item(i), TestClass).Name)
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLine(DirectCast(arr.Item(i), TestClass).Name)
Next
End Sub
hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
你好,我去过玩排序我的arraylist并有一些麻烦。也许只是错了。我的arraylist包含
对象
和对象的一个​​成员是''name。''我想根据object.name排序
arraylist - 这可能吗?
谢谢!
Derek
Hi there, I have been playing with sorting my arraylist and having some
troubles. Maybe just going about it wrong. My arraylist contains
objects
and one of the members of the object is ''name.'' I would like to sort the
arraylist based on object.name - is that possible?
Thanks!
Derek




这篇关于ArrayList.Sort问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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