|
-
Jun 18th, 2011, 08:07 AM
#1
[RESOLVED] sort array by repeat value
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...
-
Jun 18th, 2011, 08:45 AM
#2
Re: sort array by repeat value
Sorting arrays is one of most popular topics. Have a look at this CodeBank submission by Ellis Dee.
-
Jun 18th, 2011, 08:46 AM
#3
Re: sort array by repeat value
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
Last edited by Spoo; Jun 18th, 2011 at 08:50 AM.
-
Jun 18th, 2011, 08:58 AM
#4
Re: sort array by repeat value
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
-
Jun 18th, 2011, 09:06 AM
#5
Re: sort array by repeat value
bubble sort works fine with alphabetically too, thanks rhino and spoo
-
Jun 18th, 2011, 09:29 AM
#6
Re: sort array by repeat value
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
Code:
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
For simplicity, I have hardwired array dimensions to match your
example. Natch, you'll need to make your algo a little more flexible
Hope that gives you some ideas
Spoo
Last edited by Spoo; Jun 18th, 2011 at 09:40 AM.
Reason: to clarify what "that" is, seeing as you snuck in another post
-
Jun 18th, 2011, 10:32 AM
#7
Re: sort array by repeat value
spoo, thanks for the effort
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
|