我需要帮助排序arraylist(VB.net) [英] I Need Help with sorting an arraylist (VB.net)

查看:98
本文介绍了我需要帮助排序arraylist(VB.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个arraylist,在

分页,搜索&之后,从数据库中动态获取它的值。更多在ASP.net文件中。

我试图做的是对ArrayList中排序的结果进行排序,

每次都被另一个字段排序。

ArrayList包含我构建的类的列表,它适合这些

结果。

我被告知使用IComparer和sort方法,在我尝试只是

ArrayList1.Sort后出现以下错误:

System.ArgumentException:至少有一个对象必须实现IComparable

尝试给Sort方法一个IComparable参数,它仍然给我

同样的错误。

如何在没有构建的情况下对ArrayList进行排序它的完整界面? (还是

这是唯一的方法吗?)


提前谢谢。

解决方案

eLisHa< eL **** @ discussion.microsoft.com>写道:

我有一个arraylist,在
分页,搜索&之后,从数据库中动态获取它的值。更多在ASP.net文件中。
我想尝试做的是,每次由另一个字段对在ArrayList中排序的结果进行排序。
ArrayList包含一个列表我建立的类,适合这些
结果。
我被告知使用IComparer与sort方法,在我尝试只是
ArrayList1.Sort我得到以下错误:
System.ArgumentException:至少有一个对象必须实现IComparable

尝试给Sort方法一个IComparable参数,它仍然给我带来同样的错误。
如何在不构建完整接口的情况下对ArrayList进行排序? (或者它是唯一的方式吗?)




你能发一个简短但完整的程序来演示

问题吗?


请参阅 http: //www.pobox.com/~skeet/csharp/complete.html 了解详情

我的意思是什么。


- -

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

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


eLisHa,


你知道在MSDN上的Arraylist排序是一个完整的样本,在我的

意见中描述了你的问题。

http://msdn.microsoft.com/library/de...ssort topic.asp


我希望这会有所帮助,


Cor


< blockquote>好的那么......关于那篇MSDN的文章,是的,我已经读过很多次了......

从来没有帮助过我。

我的网页就像这个:

页面,一个搜索结果页面,将搜索crietenia作为QuerySting获取BindData Sub,它将所需的数据添加到asp.net控件。它主要从数据库中获取完整的表,并在其中搜索所请求的文本等。当找到结果时,它将作为折扣添加到我的全局ArrayList中。示例:


公共类折扣

私有iID为整数,iName为字符串,iBus为字符串

Sub New(sID As整数,sName为String,sBus为String)

''这将创建Discount类的新实例,获取有关折扣的所有信息



''例如:

Me.iID = sID

Me.iName = sName

Me.iBus = sBus

结束子


Public ReadOnly Property ID()As String

获取

返回iID

结束获取

结束物业

Public ReadOnly物业名称()作为字符串

获取

返回iName

结束获取

结束物业

Public ReadOnly Property Bus()As String

获取

返回iBus

结束获取

结束物业


结束课程

To添加一个折扣到全局ArrayList,我使用Add方法:
ArrayList1.Add(新折扣(drDiscount(" id"),drDiscount(" type_name"),

(" Business")))

BindData()完成后,有一个分页功能,从ViewState获取pageId值。



我的客户希望我为结果构建一个排序方法。当然,它在分页之前运行了

,所以分页是正确的。


所以我需要帮助的是,构建Sort方法,这将适用于当前折扣房产的任何




我希望我的问题现在更容易理解。

i have an arraylist that gets it''s values dynamiclly from the database, after
paging, searching & more in the ASP.net file.
What im trying to do, is sort the results that are sorted in the ArrayList,
each time by another field.
The ArrayList contains a list of a class which i built, that fits these
results.
I was told to use a IComparer with the sort method, after i try''ed just
ArrayList1.Sort i got the following error:
System.ArgumentException: At least one object must implement IComparable

try''ed giving the Sort method an IComparable parameter, it still gives me
the same error.
How can i sort my ArrayList without building a full interface for it? (or is
it the only way?)

thanks in advance.

解决方案

eLisHa <eL****@discussions.microsoft.com> wrote:

i have an arraylist that gets it''s values dynamiclly from the database, after
paging, searching & more in the ASP.net file.
What im trying to do, is sort the results that are sorted in the ArrayList,
each time by another field.
The ArrayList contains a list of a class which i built, that fits these
results.
I was told to use a IComparer with the sort method, after i try''ed just
ArrayList1.Sort i got the following error:
System.ArgumentException: At least one object must implement IComparable

try''ed giving the Sort method an IComparable parameter, it still gives me
the same error.
How can i sort my ArrayList without building a full interface for it? (or is
it the only way?)



Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

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


eLisHa,

You know that at Arraylist sort on MSDN is a complete sample that in my
opinion describes your question.

http://msdn.microsoft.com/library/de...ssorttopic.asp

I hope this helps,

Cor


Ok then... about that MSDN article, yeah, ive read it lots of times... it
never help me though.
My web page works like this:

Page, a search results page, gets search crietenia as a QuerySting
BindData Sub, which adds the needed data to the asp.net controls. It mainly gets the full tables from the database and searchs in them for the requested text, etc. When a result is found, it is added to my global ArrayList, as a Discount. example:
Public Class Discount
Private iID As Integer, iName As String, iBus As String
Sub New(sID As Integer, sName As String, sBus As String)
'' This creats a new instance of the Discount class, gets all information
needed about the discount.
'' for instance:
Me.iID = sID
Me.iName = sName
Me.iBus = sBus
End Sub

Public ReadOnly Property ID() As String
Get
Return iID
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return iName
End Get
End Property
Public ReadOnly Property Bus() As String
Get
Return iBus
End Get
End Property

End Class
To add a discount to the global ArrayList, i use the Add method: ArrayList1.Add(New Discount(drDiscount("id"), drDiscount("type_name"),
("Business")))
After the BindData() Finishes, there is a function for paging, which gets it pageId value from ViewState.


My client wants me to build a sort method for the results. ofcourse it has
to run before the paging one, so the paging will be right.

So what i need help in, is building that Sort method, which will work on ANY
of the currect discount property''s.

I hope my question is easier to understand now.


这篇关于我需要帮助排序arraylist(VB.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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