hi experts,
i need to sort an array by repeated values, any idea or example?
say for example
if array value is
A,B,C,B,A,B,C
result shuld be
A,A,B,B,B,C,C
thanks
with regards
seenu...
Printable View
hi experts,
i need to sort an array by repeated values, any idea or example?
say for example
if array value is
A,B,C,B,A,B,C
result shuld be
A,A,B,B,B,C,C
thanks
with regards
seenu...
Sorting arrays is one of most popular topics. Have a look at this CodeBank submission by Ellis Dee.
Seenu
Your request is a little ambiguous ...
- Do you just want to sort "alphabetically"?
- Do you want to sort based on the "seed" -- eg:
- A,B,C,B,A,B,C becomes A,A,B,B,B,C,C
- C,B,A,B,C,B,A becomes C,C,B.B.B,A,A
- Will there always be only 1 letter?
EDIT:
BTW, I second RhinoBull's suggestion
Spoo
thanks for the reply,
spoo,
not alphabetically, i just shown for example, i need to sort by repeat values of any string
just like
seenu
spoo
rhino
spoo
rhino
seenu
result wil be
seenu
seenu
spoo
spoo
rhino
rhino
bubble sort works fine with alphabetically too, thanks rhino and spoo
Seenu
OK, then that (your post #4) is what I would refer to as -- based on a "seed"
That is, based on the chronological order each unique value is encountered,
and then "grouped".
Here is something that springs to mind -- concept:
- Go through the list, store unique values in chronological order
as they are encountered in a 2-D array- Also keep track of how many times repeat values are encountered
- Then dump the contents into a 1-D array, grouped chronologically
For simplicity, I have hardwired array dimensions to match yourCode:Dim aaSrc(6)
Dim aaUni(3, 2)
Dim aaDump(6)
' 1. populate aaSrc
aaSrc(1) = "seenu"
aaSrc(2) = "spoo"
aaSrc(3) = "rhino"
aaSrc(4) = "spoo"
aaSrc(5) = "rhino"
aaSrc(6) = "seenu"
' 2. populate unique
uni = 0 ' default unique counter
For ii = 1 to 6
hv = 0 ' "have a match" flag
' 2.1. have a match
For jj = 1 to 3
If aaUni(jj, 1) = aaSrc(ii) Then
hv = 1 ' set flag
aaUni(jj, 2) = aaUni(jj, 2) + 1 ' increment count of this name
End If
Next jj
' 2.2. new name encountered
If hv = 0 Then
uni = uni + 1
aaUni(uni, 1) = aaSrc(ii) ' 1 name
aaUni(uni, 2) = 1 ' 2 count
End If
Next ii
' 3. populate dump
nn = 0
For ii = 1 to 3
ct = aaUni(ii, 2)
For jj = 1 to ct
nn = nn + 1
aaDump(nn) = aaUni(ii, 1)
Next jj
Next ii
example. Natch, you'll need to make your algo a little more flexible
Hope that gives you some ideas
Spoo
spoo, thanks for the effort