Results 1 to 14 of 14

Thread: [RESOLVED] Sort the elements of a collection

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Sort the elements of a collection

    I have a collection of integer values. What would be the best way to sort them in ascending order?

    The problem is I can't just assign a collection element the value of another element, i.e. this isn't allowed:

    col.item(i) = col.item(j)

    I'm sure it has a straightforward way to deal with but I don't seem to be tuned in to the right wavelength.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Sort the elements of a collection

    This might possibly ( hopefully ) help :

    http://www.source-code.biz/snippets/vbasic/6.htm
    VB.NET MVP 2008 - Present

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,747

    Re: Sort the elements of a collection

    If you only store numeric values, why don't you use an array?

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Sort the elements of a collection

    Probably not the best way, but at least, the following is quite straightforward:

    Code:
    Public Sub SortAscIntCol(ByRef Col As Collection)
        Dim i As Long, j As Long, k As Integer
    
        For i = 2& To Col.Count
            k = Col(i)
    
            For j = i - 1& To 1& Step -1&
                If k >= Col(j) Then
                    Col.Remove i
                    Col.Add k, After:=j
                    GoTo 1              '<-- For efficiency reasons
                End If
            Next j
    
            Col.Remove i
            Col.Add k, Before:=1&
    1   Next i
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: Sort the elements of a collection

    Quote Originally Posted by Arnoutdv View Post
    If you only store numeric values, why don't you use an array?
    this

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Sort the elements of a collection

    Quote Originally Posted by HanneSThEGreaT View Post
    This might possibly ( hopefully ) help :

    http://www.source-code.biz/snippets/vbasic/6.htm
    Thank you.

    I understand heapsort becomes ever more efficient as the amount of items to be sorted grows larger.

    I really don't have that many items in the collection so maybe Bonnie West's solution is easier to understand and fast enough. But I add the heapsort code to my code bank.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Sort the elements of a collection

    No problem at all I hope you come right. Good luck!
    VB.NET MVP 2008 - Present

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Sort the elements of a collection

    Quote Originally Posted by Arnoutdv View Post
    If you only store numeric values, why don't you use an array?
    The reasons to use a collection outweigh the reasons not to use it.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Sort the elements of a collection

    Quote Originally Posted by Bonnie West View Post
    Probably not the best way, but at least, the following is quite straightforward:
    ...etc...
    Thank you!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sort the elements of a collection

    Olaf posted an intriguing way to sort using something called vbRichClient. You can check it out here. I've never used that before but I like its relative simplicity. Beats having to implement your own sort.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Sort the elements of a collection

    Quote Originally Posted by Niya View Post
    Olaf posted an intriguing way to sort using something called vbRichClient. You can check it out here. I've never used that before but I like its relative simplicity. Beats having to implement your own sort.
    Interesting but I didn't know about this vbRichClient5 thing nor about the cArrayList object. I couldn't spot them among the References and Components.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sort the elements of a collection

    Well I've never used it but I've seen countless posts about it here on VBForums. From what I've seen, it seems to be some kind of library that can aid in improving developer productivity by providing all kinds of interesting and modern components so you don't have to re-invent the wheel for a lot of things. As intriguing as it is, it wasn't enough for me to go and research it or try it out since I don't use VB6 anymore so I'm not much qualified to talk about it. Dilettante knows about at it. I think Bonnie knows about it too, maybe they can chime in on this.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,897

    Re: Sort the elements of a collection

    You can get vbRichClient5 from http://vbrichclient.com/#/en/Downloads.htm.

    Download and unpack the ZIP file, then register the vbRichClient5.dll using regsvr32. It will then appear in your references list in VB6.

    When distributing your application, make sure to include all of the DLL files that shipped with the ZIP that you download, and register vbRichClient5.dll on the computer that you are installing to as part of your installation package.

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: Sort the elements of a collection

    Quote Originally Posted by krtxmrtz View Post
    Interesting but I didn't know about this vbRichClient5 thing nor about the cArrayList object. I couldn't spot them among the References and Components.
    jpbro already posted, where the download is located.

    As for "Sorting with vbRichClient5-Classes"...

    The cArrayList has an internal (QuickSort-based) sorting and is the most lightweight of the bunch.
    It encapsulates only a true SafeArray, and thus a vbByte-typed cArrayList only allocates as much memory as an appropriate "normal VB-ByteArray" -
    though other than VBs normal Arrays, it offers methods to insert/remove Values somewhere in-between - and it also offers Stack-like Push/Pop, Queue/DeQueue-methods.


    Then there's the cSortedDicitionary, which implements a "Sorting-whilst-adding" approach - and is thus always already sorted
    (in fact the Keyed-access into this Container-Class doesn't work Hash-based, but *depends* on "being always already sorted").
    It allows (aside from the usual "String-Keys") also Integer-based Key-Values (Byte/Integer/Long/Currency) - but also Date/Double/Single-Typed Keys are allowed.
    Sort-performance is comparable to a good QuickSort-algo - and it outperforms the MS-Scripting-Dicitionary as well as the VB-Collection in
    all disciplines (Item-Retrieval per Key, Adding/Removing per Key, etc.) - it has a KeyExists-Method and also allows a fast enumeration of the Keys.


    Last one in the bunch is the cCollection which is basically a "cSortedDictionary with an additional Index, which holds the Add-Order too".
    So, also here - the Items are always "kept ordered by Key" - but their "original Add-Order" is also kept.
    Later on you can enumerate (Keys or Items, or both) either in Key-Sort-Order, or in Add-Order.
    In case of String-Typed-Keys, then in the same way as with the cArrayList, the cCollection supports different locale-ID and language-settings
    for its internal String-Key-Sorting.


    Well - and since we are at the different Container-Classes of the RC5 - there's also always the cRecordset-Class, which can be used
    in conjunction with an InMemory-DB, then allowing all kinds of SQL-Operations (advanced Filtering/Sorting/Grouping) for "Multi-Column-Data" -
    "Uploads" into the InMemory-DB are pretty fast (depending on the Column-Count, but about 300000-500000 Records per Second are normal).

    So the latter Container-class allows (in conjunction with cMemDB) a kind of "LINQ-like querying" for complex InApp (InMemory) data.

    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width