Sorting inside a sorted list?
BlitzMax Forums/BlitzMax Beginners Area/Sorting inside a sorted list?
| ||
| Hello! This one is giving me a headache: In my app I use the following simple type construct to get a sorted list of elements.
Type sortdfa_type
Field id: Int
Field z:Int
Field av_c:Int
Method compare:Int(v:Object)
Return sortdfa_type(v).z-z
EndMethod
EndType
' Sorting...
SortArray.sort(False)
SortArray.sort(True)
Each entry has an id (=the static array index) and a percentage value. The result looks like this: ![]() What I would like to have is that the entries of the same percentage (e.g. 100% on the screen) get sorted "inside" as well. Means all entries of the same percentage get sorted after their id number/field. So I have: 001 name (100 %) 006 name (100 %) 031 name (100 %) (...) 033 name (99 %) 040 name (99 %) 041 name (99 %) (...) and so on. How can I sort my type list to get such results without "destroying" their proper "overal" order? Thanks, Grisu P.S.: The faster the sorting algo the better as I have to do this realtime! :( |
| ||
| You need to do a 2 way comparision. First check for z and if the difference is 0 then apply a second sorting rule which does the internal sorting. |
| ||
Ok, this isn't fast, but it seems to work???
Method compare:Int(v:Object)
If sortdfa_type(v).z-z = 0 Then
Return sortdfa_type(v).id-id
Else
Return sortdfa_type(v).z-z
EndIf
EndMethod
|
| ||
Can't you just do this instead of sorting it twice?
Method compare:Int(v:Object)
If sortdfa_type(v).z = z Then
Return sortdfa_type(v).id - id
Else
Return z - sortdfa_type(v).z
EndIf
EndMethod
And then sort it only once in ascending order. |
| ||
| I only sort the array one time depending in which order the user wants the output (upwards/downwards). Your code doesn't work for me, but I managed to take some math out which is good. Thanks for the hint.
Method compare:Int(v:Object)
If sortdfa_type(v).z = z Then
Return sortdfa_type(v).id-id
Else
Return sortdfa_type(v).z-z
EndIf
EndMethod
|
| ||
| Well no prob. Every little bit helps I guess. |
