H!
;i'm searching for a fast way to order a type
;order low ... high
;output: 4 5 7
Type monkey
Field n%
End Type
bla.monkey = New Monkey
bla\n% = 5
bla.monkey = New Monkey
bla\n% = 7
bla.monkey = New Monkey
bla\n% = 4
For bla.monkey = Each Monkey
temp% = bla\n%
For bla.monkey = Each Monkey
If temp% ....?
EndIf
Next
Next
WaitKey
The king of Sorts is the QuickSort. There is an example in the CodeArcs, but it is working with an array rather than a Type.
http://www.blitzbasic.com/codearcs/codearcs.php?code=61The principle on which it operates is a recursive binary chop {split the list into two halfs (see which is biggest), then split each half into two halfs.... keep going until you are comparing individual items} The code above has the 'difficult bit' coded (the recursive routine) so it should be easy to adapt to work with types.
A thanks GfK I forgot to search in the code archives with the keyword sort.
@BlackJumper
I will use the type script as an example but the array sort is nice too (maybe I can use that later)
See ya,
GC-Martijn
Now I'm testing and its working fast :)
My second [other question] is now:
Can I do this
Afunction(a type field)
for example the code to order a type dynamic by yust selecting a field.
I know that I can't do Afunction(obj\Index%) because i get the value then.
I tryed something with the undocumented Object. but that's wasn't working either.
Is this possible or must I make 2 function ?
Thanks
GC-Martijn
[code]
Type Item
Field Index%
Field otherField%
End Type
InsertionSort(Index%) ;Index%||otherField%
ShowData()
Function InsertionSort(what)
Local Item.Item, NextItem.Item
Local p.Item, q.Item
Local temp%
NextItem=After First Item
While NextItem<>Null
Item=NextItem
NextItem=After Item
p=Item : temp=Item\Index
Repeat ;--^------ dynamic (what)
q=Before p
If q=Null Then Exit
If temp >= q\Index Then Exit
p=q ;--^------ dynamic (what)
Forever
q=Item
Insert q Before p
Wend
End Function
[/cod]
Just pass the type handle and modify the specific field from within the function, like:
.
.
.
a.item = New item
Afunction(a)
Function Afunction(this.item)
this\index = 1
End Function