-
Jul 16th, 2018, 03:51 AM
#1
Thread Starter
Banned
[RESOLVED] Sort UDT array
Can you recommend an algorithm or idea on how to sort a UDT array with a max. of several hundred elements? The array needs to be sorted on one of the values in the type, which is a long, and contains duplicates.
-
Jul 16th, 2018, 09:58 AM
#2
Re: [RESOLVED] Sort UDT array
You marked the thread as resolved, is it really resolved?
You can use any sort algoritm you like.
I most of the times use ShellSort, for both it's simplicity and speed.
Code:
Option Explicit
Private Type tpUDT
lValue As Long
iValue As Integer
btValue As Byte
End Type
Private Sub ShellSortUDT(tUDT() As tpUDT)
Dim lLBound As Long, lUBound As Long
Dim lLoop As Long, lHold As Long, lHValue As Long
Dim tTemp As tpUDT
lLBound = LBound(tUDT)
lUBound = UBound(tUDT)
lHValue = lLBound
Do
lHValue = 3 * lHValue + 1
Loop Until lHValue > lUBound
Do
lHValue = lHValue / 3
For lLoop = lHValue + lLBound To lUBound
tTemp = tUDT(lLoop)
lHold = lLoop
Do While tUDT(lHold - lHValue).lValue > tTemp.lValue
tUDT(lHold) = tUDT(lHold - lHValue)
lHold = lHold - lHValue
If lHold < lHValue Then Exit Do
Loop
tUDT(lHold) = tTemp
Next lLoop
Loop Until lHValue = lLBound
End Sub
-
Jul 16th, 2018, 11:45 AM
#3
Re: [RESOLVED] Sort UDT array
Brent,
The first thing you'd need to decide is the sort-key. I'm assuming it's one of the fields in the UDT. Once that's decided, the rest is rather trivial. I've got a quick-sort-like algorithm I often use, but Arnoutdv's also looks okay at first glance.
Once you know what you're sorting on, just modify the algorithm to deal with your UDT, and you're done. The algorithm would be specific to that UDT, but that's just the way it's going to be with UDTs.
Good Luck,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jul 16th, 2018, 12:56 PM
#4
Re: [RESOLVED] Sort UDT array
BrentM.Adams36, it's faster not to sort UDT, instead: you can prepare sorting order (stored in another array, let's say "pos()" ) using one of the field (as suggested by @Elroy) as a key for sorting.
Next when you need to use UDT, you apply "pos()" to your UDT array, e.g.:
Code:
for i = 0 to Ubound(pos)
debug.? UDT(pos(i))
next
Sorting example:
Code:
'Sorting user type array using bufer array of positions (c) Dragokas
Dim pos() As Long, names() As String: ReDim pos(cnt), names(cnt)
For i = 0 To cnt: pos(i) = i: names(i) = UDT(i).DisplayName: Next 'DisplayName - here is an example of the field of your UDT used as a key for sorting
QuickSortSpecial names, pos, 0, cnt
Helper func.:
Code:
Public Sub QuickSortSpecial(j() As String, k() As Long, ByVal low As Long, ByVal high As Long)
Dim i As Long, l As Long, M As String, wsp As String
i = low: l = high: M = j((i + l) \ 2)
Do Until i > l: Do While j(i) < M: i = i + 1: Loop: Do While j(l) > M: l = l - 1: Loop
If (i <= l) Then wsp = j(i): j(i) = j(l): j(l) = wsp: wsp = k(i): k(i) = k(l): k(l) = wsp: i = i + 1: l = l - 1
Loop
If low < l Then QuickSortSpecial j, k, low, l
If i < high Then QuickSortSpecial j, k, i, high
End Sub
-
Jul 16th, 2018, 02:25 PM
#5
Re: [RESOLVED] Sort UDT array
To all. This may be a suspected bot. The user has posted multiple threads throughout the forum evidently. Here's a quote from on of the threads in the "Chit Chat" forum. I wouldn't waste any more effort on this thread unless the OP replies back.
This is someone, maybe a bot, trying to build up a post count for some nefarious reason. I've already reported it to the mods.
-
Jul 16th, 2018, 04:15 PM
#6
Re: [RESOLVED] Sort UDT array
Originally Posted by LaVolpe
To all. This may be a suspected bot.
Hello. What do you mean a bot? An artificial intelligence program?
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
|