|
-
Oct 16th, 2013, 09:40 AM
#1
[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)
-
Oct 16th, 2013, 10:07 AM
#2
Re: Sort the elements of a collection
VB.NET MVP 2008 - Present
-
Oct 16th, 2013, 11:03 AM
#3
Re: Sort the elements of a collection
If you only store numeric values, why don't you use an array?
-
Oct 16th, 2013, 12:56 PM
#4
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)
-
Oct 16th, 2013, 03:02 PM
#5
Re: Sort the elements of a collection
 Originally Posted by Arnoutdv
If you only store numeric values, why don't you use an array?
this
-
Oct 17th, 2013, 03:11 AM
#6
Re: Sort the elements of a collection
 Originally Posted by HanneSThEGreaT
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)
-
Oct 17th, 2013, 03:14 AM
#7
Re: Sort the elements of a collection
No problem at all I hope you come right. Good luck!
VB.NET MVP 2008 - Present
-
Oct 17th, 2013, 03:37 AM
#8
Re: Sort the elements of a collection
 Originally Posted by Arnoutdv
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)
-
Oct 17th, 2013, 03:38 AM
#9
Re: Sort the elements of a collection
 Originally Posted by Bonnie West
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)
-
Oct 17th, 2013, 03:49 AM
#10
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.
-
Oct 17th, 2013, 04:38 AM
#11
Re: Sort the elements of a collection
 Originally Posted by Niya
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)
-
Oct 17th, 2013, 04:43 AM
#12
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.
-
Oct 17th, 2013, 09:53 AM
#13
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.
-
Oct 18th, 2013, 03:28 AM
#14
Re: Sort the elements of a collection
 Originally Posted by krtxmrtz
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|