VB6 的 IComparable 排序等价物

Posted

技术标签:

【中文标题】VB6 的 IComparable 排序等价物【英文标题】:IComparable sorting equivalent for VB6 【发布时间】:2011-08-23 00:15:41 【问题描述】:

有没有人在 VB6 中遇到/创建了一个体面的对象集合通用排序实现?

如果有,有人愿意提供代码或链接吗?

【问题讨论】:

要对这些“对象”进行排序,您会使用什么作为排序键? 嗯,我想这就是重点,需要某种方式来指定... 【参考方案1】:

这个对我有用。

请注意,我不是作者。 Function 标头中提到了原始来源,但该站点现在似乎已经消失了。

实现它的部分是 VB 鲜为人知或经常被忽视的CallByName 命令。

Public Function SortItemCollection(col As Collection, ByVal sPropertyName As String, _
   ByVal bolSortAscending As Boolean, ByVal bolCompareNumeric As Boolean) As Collection
'------------------------------------------------------------------------------
'Purpose  : Sort a collection of objects using one of the object's properties
'           as the sorting field. That property must be of a primitive
'           data type (string or numeric)
'
'Prereq.  : !!! Important !!! The scope of property sPropertyName needs to be
'           declared as Public.
'Parameter: -
'Returns  : -
'Note     : The idea is to have a class that is added to a collection object.
'           Pass that collection to this function below and the property name
'           is the “field” within the class that is to be sorted on.
'
'   Author: Original author unknown, refined by Branko Pedisic
'   Source: http://www.ifnottruethenfalse.com/sort-a-collection-object-in-vb6/
'  Changed: 19.03.2014
'           - Source reformatted and variable names changed to accommodate my
'           naming conventions.
'------------------------------------------------------------------------------
   Dim colNew As Collection
   Dim oCurrent As Object
   Dim oCompare As Object
   Dim lCompareIndex As Long
   Dim sCurrent As String
   Dim sCompare As String
   Dim bolGreaterValueFound 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 oCurrent In col

      'get value of current item...
      sCurrent = CallByName(oCurrent, sPropertyName, VbGet)

      'setup for compare loop
      bolGreaterValueFound = False
      lCompareIndex = 0

      For Each oCompare In colNew
         lCompareIndex = lCompareIndex + 1

         sCompare = CallByName(oCompare, sPropertyName, VbGet)

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

            If (bolSortAscending = True) Then
               If Val(sCurrent) < Val(sCompare) Then
                  'found an item in compare collection that is greater...
                  'add it to the new collection...
                  bolGreaterValueFound = True
                  colNew.Add oCurrent, , lCompareIndex
                  Exit For
               End If
            Else
               If Val(sCurrent) > Val(sCompare) Then
                  'found an item in compare collection that is greater...
                  'add it to the new collection...
                  bolGreaterValueFound = True
                  colNew.Add oCurrent, , lCompareIndex
                  Exit For
               End If
            End If

         Else     '// If bolCompareNumeric = True
            'this means we are looking for a string sort...

            If (bolSortAscending = True) Then
               If sCurrent < sCompare Then
                  'found an item in compare collection that is greater...
                  'add it to the new collection...
                  bolGreaterValueFound = True
                  colNew.Add oCurrent, , lCompareIndex
                  Exit For
               End If
            Else
               If sCurrent > sCompare Then
                  'found an item in compare collection that is greater...
                  'add it to the new collection...
                  bolGreaterValueFound = True
                  colNew.Add oCurrent, , lCompareIndex
                  Exit For
               End If
            End If

         End If   '// If bolCompareNumeric = True
      Next oCompare

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

   Next oCurrent

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

End Function

【讨论】:

以上是关于VB6 的 IComparable 排序等价物的主要内容,如果未能解决你的问题,请参考以下文章

这个c#程序,那行list.sort(),怎么就对他们的年龄进行排序了呢,也没说明根据啥排啊

C#排序比较

C#中常用接口介绍

这个匿名对象没有实现IComparable接口

C#中的IComparable和IComparer接口

C#中的IComparable和IComparer接口