|
-
Nov 28th, 2005, 03:25 PM
#1
Thread Starter
Addicted Member
[RESOLVED] sorting arrays
I have an array called iarray(), whihc contains the members iarray(0) to array(4). is there a way of sorting out the higher (in terms of the array) array to the lower?
i.e. The arrays are randomly placed in the following order:
iarray(3)
iarray(2)
iarray(4)
iarray(1)
iarray(0)
I would like the code to sort it so it goes from iarray(4) to iarray(0),and then put the indexes (EDIT: indicies) into a new array called order()
-
Nov 28th, 2005, 03:37 PM
#2
Re: sorting arrays
A better explaination is in order. Do you mean in number sequence? (4)(3)... or the contents of the array?
-
Nov 28th, 2005, 03:40 PM
#3
Thread Starter
Addicted Member
Re: sorting arrays
in reverse sequence (number of the array)
-
Nov 28th, 2005, 03:47 PM
#4
Re: sorting arrays
VB Code:
For i = Ubound(isarray) to LBound(isarray) Step -1
Order(j) = isarray(i)
j = j + 1
Next
Last edited by randem; Nov 29th, 2005 at 12:38 PM.
-
Nov 29th, 2005, 11:43 AM
#5
Thread Starter
Addicted Member
Re: sorting arrays
how can I ensure that the random values in iarray() have been sorted into descending order? I've tried putting a msgbox in the code (just before j=j+1) but a message just pops up saying "0".
-
Nov 29th, 2005, 11:48 AM
#6
Re: sorting arrays
Here is code that sorts arrays: havent tried it with numbers but it should work..
Make a copy of the random array.. sort the copy..
then compare Sorted to original.. creating an array with the index order?
sounds like it might work... but, there is probably a better way lol
sort code:
VB Code:
'Author: CVMichael
'Thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
Private Sub QuickSort(C() As String, ByVal First As Long, ByVal Last As Long)
Dim Low As Long, High As Long
Dim MidValue As String
Low = First
High = Last
MidValue = C((First + Last) \ 2)
Do
While C(Low) < MidValue
Low = Low + 1
Wend
While C(High) > MidValue
High = High - 1
Wend
If Low <= High Then
Swap C(Low), C(High)
Low = Low + 1
High = High - 1
End If
Loop While Low <= High
If First < High Then QuickSort C, First, High
If Low < Last Then QuickSort C, Low, Last
End Sub
Private Sub Swap(ByRef A As String, ByRef B As String)
Dim T As String
T = A
A = B
B = T
End Sub
Last edited by Static; Nov 29th, 2005 at 01:11 PM.
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 29th, 2005, 11:53 AM
#7
Thread Starter
Addicted Member
Re: sorting arrays
I didnt understand that. I couldnt figure out where any of the variables went, and I couldnt ensure that any of it worked. Thanks anyway. Could someone try to work on randem's code so it will msgbox the contents of the "order" array?
-
Nov 29th, 2005, 12:15 PM
#8
Re: sorting arrays
 Originally Posted by ajames
I didnt understand that. I couldnt figure out where any of the variables went, and I couldnt ensure that any of it worked.
The original thread for that code is here:
VB - Quick Sort algorithm (very fast sorting algorithm)
You will understand it better if you look at the thread.
You have to call the QuickSort like this:
QuickSort MyArray, LBound(MyArray), UBound(MyArray)
But the QuickSort function is made for strings, so to make it work for numbers, just replace the "As String" to "As Integer" everywhere in the code.
-
Nov 29th, 2005, 01:11 PM
#9
Re: sorting arrays
CVMichael: Cool.. I had forgotten where I got that code its GREAT!
(Added Comment in my codebank snippet stating u are the author)
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 30th, 2005, 11:47 AM
#10
Thread Starter
Addicted Member
Re: sorting arrays
CVMichael: Does that code sort the contents of the array of the indexes of the arrays?
-
Nov 30th, 2005, 12:06 PM
#11
Re: sorting arrays
 Originally Posted by ajames
CVMichael: Does that code sort the contents of the array of the indexes of the arrays?
Well, it sorts the contents of the array.
But... you can't really sort the index of the array, not with a regular array anyways...
An index is always sequention (in order), if you re-order the index, you loose the whole purpose of an index...
But I think I understand what you need.
You probably want to do something like an array of pointers (equivalent in C/C++)
To do that in VB you need an extra array to store the position of the item in the other array, Is that what you want ?
I think I can modify the QuickSort function to do that, but first I want to know if what I previously said is right ?
-
Nov 30th, 2005, 12:42 PM
#12
Re: sorting arrays
Actually, re-reading your first post, I see that that's what you want...
I modified the code, is it right ?
VB Code:
Option Explicit
Private Type tQuickSort
Index As Long
Value As Integer
End Type
Private Sub Form_Load()
Dim MyArray(10) As Integer, ArrOrder() As Long, K As Long
MyArray(0) = 23
MyArray(1) = 345
MyArray(2) = 2312
MyArray(3) = 2
MyArray(4) = 25
MyArray(5) = 66
MyArray(6) = 77
MyArray(7) = 99
MyArray(8) = 55
MyArray(9) = 345
MyArray(10) = 232
QuickSort MyArray, ArrOrder
Debug.Print " Index", " Order", " Value"
For K = LBound(MyArray) To UBound(MyArray)
Debug.Print K, ArrOrder(K), MyArray(ArrOrder(K))
Next K
End Sub
Private Sub QuickSort(iArray() As Integer, Order() As Long)
Dim K As Long
Dim tArray() As tQuickSort
ReDim tArray(LBound(iArray) To UBound(iArray))
For K = LBound(iArray) To UBound(iArray)
tArray(K).Index = K
tArray(K).Value = iArray(K)
Next K
QuickSortRecurse tArray, LBound(iArray), UBound(iArray)
ReDim Order(LBound(iArray) To UBound(iArray))
For K = LBound(tArray) To UBound(tArray)
Order(K) = tArray(K).Index
Next K
End Sub
Private Sub QuickSortRecurse(C() As tQuickSort, ByVal First As Long, ByVal Last As Long)
'
' Made by Michael Ciurescu (CVMichael from vbforums.com)
' Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
'
Dim Low As Long, High As Long
Dim MidValue As tQuickSort
Low = First
High = Last
MidValue = C((First + Last) \ 2)
Do
While C(Low).Value < MidValue.Value
Low = Low + 1
Wend
While C(High).Value > MidValue.Value
High = High - 1
Wend
If Low <= High Then
If Low <> High Then Swap C(Low), C(High)
Low = Low + 1
High = High - 1
End If
Loop While Low <= High
If First < High Then QuickSortRecurse C, First, High
If Low < Last Then QuickSortRecurse C, Low, Last
End Sub
Private Sub Swap(ByRef A As tQuickSort, ByRef B As tQuickSort)
Dim T As tQuickSort
T = A
A = B
B = T
End Sub
Last edited by CVMichael; Nov 30th, 2005 at 12:58 PM.
-
Nov 30th, 2005, 03:09 PM
#13
Re: sorting arrays
Isn't this a bit over-complicated???? What was wrong with post #4????
-
Nov 30th, 2005, 03:32 PM
#14
Re: sorting arrays
I don't understand how that works.... where is the sorting part ?
Can you give an example with that code ?
-
Nov 30th, 2005, 03:37 PM
#15
Re: sorting arrays
The value of the array doesn't matter. #4 just sorts the order of the array.
-
Nov 30th, 2005, 03:38 PM
#16
Re: sorting arrays
 Originally Posted by ajames
in reverse sequence (number of the array)
Well, it was not ask for it to be sorted by contents just by the index numbers in reverse...
-
Nov 30th, 2005, 03:49 PM
#17
Re: sorting arrays
I'm still confuzed...
Maybe if I see working code (example), I'll understand.
-
Nov 30th, 2005, 03:53 PM
#18
Re: sorting arrays
It is a working example. Re-read the first post carefully...
-
Nov 30th, 2005, 04:26 PM
#19
Re: sorting arrays
 Originally Posted by ajames
I would like the code to sort it so it goes from iarray(4) to iarray(0),and then put the indexes (EDIT: indicies) into a new array called order()
I don't know... I re-read it and re-read it... and I still get to the same conclusion...
He wants to sort the array, and put a indexes in a new array.
If it's the way you did it, then It does not make sence to put the index in a new array, why can't you just read the array in reverse order like this:
VB Code:
For I = UBound(iarray) to LBound(iarray) Step -1
Debug.Print iarray(I)
Next I
I'll wait for ajames respose to have a more clear explanation (hopefully)
Edit
I just realized that I did not make my code sort in reverse order, but that can be easily fixed if you just read the sorted array in reverse order like in the code above.
Last edited by CVMichael; Nov 30th, 2005 at 04:30 PM.
-
Nov 30th, 2005, 04:36 PM
#20
Re: sorting arrays
well, Sorting from iarray(4) to iarray(0) should be self explanitory. It's just a reorder in reverse (as defined).
-
Dec 1st, 2005, 01:47 AM
#21
Frenzied Member
-
Dec 1st, 2005, 01:50 AM
#22
-
Dec 1st, 2005, 02:56 AM
#23
Frenzied Member
Re: sorting arrays
sorry, my post was directed towards ajames.
-
Dec 1st, 2005, 12:32 PM
#24
Thread Starter
Addicted Member
Re: sorting arrays
ok, i seem to have made a difference to my code. Which was the one to sort out the contents of the arrays again?
EDIT: This is how far I have got:
the array 'order(4)' recieves a random number from 0 to 4 each time a button is pressed
it then makes 'iarray(4)' show the image corresponding to its random number (0 = 10, 2 is queen, 4 = ace etc.)
I want to be able to press a button, and get the array 'order' to replace the values so the highest number is in 'order(4)' and the lowest in 'order(0)'.
eg:
Contents of array 'order()':
order(0)=3
order(1)=1
order(2)=4
order(3)=0
order(4)=2
I would then like the code to reorganize it so:
order(0)=4
order(1)=3
order(2)=2
order(3)=1
order(4)=0
eg2) contents of order()
order(0)=4
order(1)=4
order(2)=4
order(3)=1
order(4)=4
sorted out:
order(0)=4
order(1)=4
order(2)=4
order(3)=4
order(4)=1
This should hopefully make sense
Last edited by ajames; Dec 1st, 2005 at 12:49 PM.
-
Dec 1st, 2005, 01:17 PM
#25
Re: sorting arrays
Now, that is TOTALLY different that what you originally asked.
So, just sort the contents of the array then step through the sorted array and move each element to a new array.
Is the contents of the array a string, number etc???
-
Dec 1st, 2005, 01:21 PM
#26
Re: sorting arrays
If they are all strings, you can just place all your info into a listbox (with the sorted property set to True), then Step through the ListBox and place the data elements back into the array.
-
Dec 1st, 2005, 01:29 PM
#27
Thread Starter
Addicted Member
Re: sorting arrays
 Originally Posted by randem
Now, that is TOTALLYSo, just sort the contents of the array then step through the sorted array and move each element to a new array.
Is the contents of the array a string, number etc???
the top bit is what i'm having trouble doing. How do I sort them?
The contents of the array are numbers
(i do realise that this is completly different to what i originally asked, but i figured out a new way of doing it)
-
Dec 1st, 2005, 01:39 PM
#28
Re: sorting arrays
Either use the code that was supplied by other members to sort the elements or place the elements into a listbox and have them sorted automatically (you would need to convert the numbers to strings first for the listbox).
-
Dec 1st, 2005, 01:47 PM
#29
Re: sorting arrays
Here:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim Order(0 To 4) As Integer, K As Long
Order(0) = 3
Order(1) = 1
Order(2) = 4
Order(3) = 0
Order(4) = 2
QuickSort Order, LBound(Order), UBound(Order) ' Sort ascending
ReverseArray Order ' reorder, so it will be descending
' print result
For K = LBound(Order) To UBound(Order)
Debug.Print "Order(" & K & ") = " & Order(K)
Next K
End Sub
Private Sub ReverseArray(A() As Integer)
Dim K As Long, Elements As Long
Dim LB As Long, UB As Long
LB = LBound(A)
UB = UBound(A)
Elements = UB - LB
For K = 0 To Elements \ 2
Swap A(LB + K), A(UB - K)
Next K
End Sub
Private Sub QuickSort(C() As Integer, ByVal First As Long, ByVal Last As Long)
'
' Made by Michael Ciurescu (CVMichael from vbforums.com)
' Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
'
Dim Low As Long, High As Long
Dim MidValue As Integer
Low = First
High = Last
MidValue = C((First + Last) \ 2)
Do
While C(Low) < MidValue
Low = Low + 1
Wend
While C(High) > MidValue
High = High - 1
Wend
If Low <= High Then
Swap C(Low), C(High)
Low = Low + 1
High = High - 1
End If
Loop While Low <= High
If First < High Then QuickSort C, First, High
If Low < Last Then QuickSort C, Low, Last
End Sub
Private Sub Swap(ByRef A As Integer, ByRef B As Integer)
Dim T As Integer
T = A
A = B
B = T
End Sub
-
Dec 1st, 2005, 01:48 PM
#30
Thread Starter
Addicted Member
Re: sorting arrays
How do I get the values of the array into the listbox? And I presume I'm correct by modifying the code to "dim order(4) as string"? (randem)
-
Dec 1st, 2005, 01:56 PM
#31
Re: sorting arrays
use List1.AddItem sStr (You have to place a listbox on your form first). And you convert the number to a string, you do not have to define it as a string.
ex.
VB Code:
List1.AddItem Format(Order(n),"00000")
-
Dec 1st, 2005, 02:04 PM
#32
Thread Starter
Addicted Member
Re: sorting arrays
Good, thats working, but how would I remove the items in the listbox before the next additem? I have tried "list1.clear" but that doesnt work: it only shows one value after that, even if there's more than one value being added
-
Dec 1st, 2005, 02:09 PM
#33
Re: sorting arrays
What???,
Don't you want to add all the items to the listbox in order to get them sorted????
Post some code...
-
Dec 1st, 2005, 02:09 PM
#34
Re: sorting arrays
Why don't you use my code ? you don't have to convert to String !
-
Dec 1st, 2005, 02:22 PM
#35
Thread Starter
Addicted Member
Re: sorting arrays
VB Code:
'after pressing a button (with extra code above)
For i = 0 To 4
List1.Clear
List1.AddItem Format(order(i), "")
Next
Thats the code i'm using.
The thing is, is this code getting all 5 parts of the array 'order()'?
Order() , as I have stated, has a random number in each part of it, so there are 5 random numbers in it.
If I put a msgbox below saying 'list1.listcount' it only has recorded 1 variable
-
Dec 1st, 2005, 02:37 PM
#36
Re: sorting arrays
Because you are clearing it 4 times. try this:
VB Code:
'after pressing a button (with extra code above)
List1.Clear
For i = 0 To 4
List1.AddItem Format(order(i), "")
Next
-
Dec 1st, 2005, 02:51 PM
#37
Thread Starter
Addicted Member
Re: sorting arrays
i have a slight problem with that. I have 5 'hold' checkboxes, so I would need a way to only clear the things that have not been held, and not do anything about the others
Is there a '.clear' option which you can specify which entry you want to delete?
-
Dec 1st, 2005, 03:20 PM
#38
Re: sorting arrays
ajames,
Your format statement will give you inacurate sorts if your numbers are longer than 1 character.
Things that have not been held???? If you do not put the items in the listbox would that be better?
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
|