VB6 的 IComparable 排序等效项

发布于 2024-11-06 12:23:20 字数 66 浏览 1 评论 0原文

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

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

Has anyone encountered/created a decent implementation of generic sorting of collections of objects in VB6?

If so, anyone care to provide code or link?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2024-11-13 12:23:20

这对我来说很有效。

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

让它运行的部分是 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

This one does the trick for me.

Please note that I'm not the author. The original source is mentioned in the Function header, but that site seems to be gone by now.

The part to get it going is VB's little known or often overlooked CallByName command.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文