really cool OO type sorting possible?
BlitzMax Forums/BlitzMax Beginners Area/really cool OO type sorting possible?
| ||
Hi, Now we have OO is it possible to.... SortList(a:type,bythisvar) ? respecting positive and negative? Or maybe do sorting faster somehow? |
| ||
You can override the Compare method for a Type, which gives you *some* control over ordering of user defined types... Not sure if this is as much control as you are talking about? :o) Brucey |
| ||
Well you could also write your own sort list that uses a passed in functor object, that can do the comparisons for you in whatever way you want, depending on how you want to sort. Aaron |
| ||
I'm really very much a beginner. Thanks for your help but I'm actually wondering if someone has done a fast sort already..? Thanks |
| ||
sorry to be a pain in the bum. But, I can't find before and after type commands. I wanted to port the quicksort by skidracer but can't. |
| ||
Type A Field SortField:String Method New() '// stuff it with some 'random' shite For Local n:Int = 0 To 10 SortField :+ Chr( Rnd( Asc("a"), Asc("z")) ) Next End Method Method Compare:Int( o:Object ) If( SortField > A(o).SortField ) Then Return 1 Else If( SortField < A(o).SortField ) Then Return -1 Else Return 0 End If End Method End Type SeedRnd( MilliSecs() ) Local arr:A[ 10 ] For Local n:Int = 0 To 9 arr[n] = New A Next '// Sort the array. Non-string or non-numeric '// values in the array are sorted by object(memory) handle, '// so not very useful. '// The type must override the native Blitz Object's '// Compare() method to offer a different comparison arr.sort( 1 ) '// 1=ascend, 0=desc For n = 0 To arr.length - 1 Print( arr[n].SortField ) Next |
| ||
The before and after command are in BlitzMax they are just hidden from you =) They are in the TList Type, which you use when you create Lists. I assume you have a type of which you created an instance called BattleCruiser, here is some commands: BattleCruiserList:TList = CreateList() * Add stuff to the list BattleCruiserList.AddFirst( BattleCruiser ) BattleCruiserList.AddLast( BattleCruiser2 ) newLink:TLink=FindLink(BattleCruiser) newLink.InsertAfterLink(BattleCruiser2) When you deal with Lists you deal with a ListType which contains LinkTypes, each TLink which in turn contains YourType. If The Links are sorted so are YourObjects. Find the right Linkm insert it before/After. If I have time I guess I'll try to make a sort-TypeList function because it is quite complicated escpecially if you are not into OOP yet, but If you already know how to do a sort-Type-list in Blitz3D then this shouldn't be impossible. If you wounder wherefrom I get all commands check out your BlitzMax folder\Mod\brl.mod\LinkedList.Mod\LinkedList.bmx |
| ||
Defiance, Nice example, but a bit redundant as strings already provide a functional 'Compare' method: Local t$[]=["Hello","There","This","Is","A","Test","Of","String","Sorting"] t.Sort For local q$=EachIn t Print q Next |