如何在集合中排序

HeatLoad 2002-04-28 03:59:27
我在 程序中用了大量的 Collection Object
我现在 需要 对这些 集合中的 对象排序, 不是 对数字排序哟!

请大虾们 代写一下代码!!!
高分 献上!
...全文
198 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
HeatLoad 2002-04-28
  • 打赏
  • 举报
回复
up
dbcontrols 2002-04-28
  • 打赏
  • 举报
回复
http://www.d1vb.com上万个VB朋友都注册了,50000条记录就是人气的象征!几千个代码和文章是你急需的。我是版主和站长,欢迎您的光顾!我们会尽最大努力帮助你。
sonicdater 2002-04-28
  • 打赏
  • 举报
回复
'Add the following to the Class
'-----------------------------------------------------------------
Option Explicit

Public ID As Long
Public NameValue As String

'Add the following to the Form
'-----------------------------------------------------------------
Option Explicit

Private col As Collection

Private Sub btnSort_Click()
Dim obj As clsTest

Set col = SortItemCollection(col, "ID", True)

List1.Clear

For Each obj In col
List1.AddItem obj.NameValue
Next

End Sub

Private Sub Form_Load()
Dim obj As clsTest

'build a collection of test objects when the form loads...

Set col = New Collection

Set obj = New clsTest
obj.ID = 6
obj.NameValue = "Item Six"
col.Add obj

Set obj = New clsTest
obj.ID = 2
obj.NameValue = "Item Two"
col.Add obj

Set obj = New clsTest
obj.ID = 3
obj.NameValue = "Item Three"
col.Add obj

Set obj = New clsTest
obj.ID = 1
obj.NameValue = "Item One"
col.Add obj

Set obj = New clsTest
obj.ID = 4
obj.NameValue = "Item Four"
col.Add obj

Set obj = New clsTest
obj.ID = 5
obj.NameValue = "Item Five"
col.Add obj

Set obj = New clsTest
obj.ID = 7
obj.NameValue = "Item Seven"
col.Add obj


For Each obj In col
List1.AddItem obj.NameValue
Next

End Sub

' ===== End =====
sonicdater 2002-04-28
  • 打赏
  • 举报
回复
'Add the following to the module
-------------------------------------------------------------------
Option Explicit

Public Function SortItemCollection(col As Collection, strPropertyName, Optional blnCompareNumeric As Boolean = False) As Collection
Dim colNew As Collection
Dim objCurrent As Object
Dim objCompare As Object
Dim lngCompareIndex As Long
Dim strCurrent As String
Dim strCompare As String
Dim blnGreaterValueFound As Boolean

'make a copy of the collection, ripping through it one item
'at a time, adding to new collection in right order...

Set colNew = New Collection

For Each objCurrent In col

'get value of current item...
strCurrent = CallByName(objCurrent, strPropertyName, VbGet)

'setup for compare loop
blnGreaterValueFound = False
lngCompareIndex = 0

For Each objCompare In colNew
lngCompareIndex = lngCompareIndex + 1

strCompare = CallByName(objCompare, strPropertyName, VbGet)

'optimization - instead of doing this for every iteration, have 2 different loops...
If blnCompareNumeric = True Then
'this means we are looking for a numeric sort order...

If Val(strCurrent) < Val(strCompare) Then
'found an item in compare collection that is greater...
'add it to the new collection...
blnGreaterValueFound = True
colNew.Add objCurrent, , lngCompareIndex
Exit For
End If

Else
'this means we are looking for a string sort...

If strCurrent < strCompare Then
'found an item in compare collection that is greater...
'add it to the new collection...
blnGreaterValueFound = True
colNew.Add objCurrent, , lngCompareIndex
Exit For
End If

End If
Next

'if we didn't find something bigger, just add it to the end of the new collection...
If blnGreaterValueFound = False Then
colNew.Add objCurrent
End If

Next

'return the new collection...
Set SortItemCollection = colNew
Set colNew = Nothing

End Function
cooler 2002-04-28
  • 打赏
  • 举报
回复
COLLECTION没有排序功能,只能自己遍历集合进行手工排序。
一种比较好的方法是在插入时就进行派逊,按照INDEX来组织。
你的说法太不详细乐,不知道怎么给出适用代码。
按对象排序?怎么判断先后?
cooler 2002-04-28
  • 打赏
  • 举报
回复
COLLECTION没有排序功能,只能自己遍历集合进行手工排序。
一种比较好的方法是在插入时就进行派逊,按照INDEX来组织。
你的说法太不详细乐,不知道怎么给出适用代码。
lily0000000 2002-04-28
  • 打赏
  • 举报
回复
在SQL语句中用ORDER BY 排序字段名

7,787

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧