Sorting inside a sorted list?

BlitzMax Forums/BlitzMax Beginners Area/Sorting inside a sorted list?

Grisu(Posted 2006) [#1]
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! :(


Dreamora(Posted 2006) [#2]
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.


Grisu(Posted 2006) [#3]
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



Cajun17(Posted 2006) [#4]
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.


Grisu(Posted 2006) [#5]
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



Cajun17(Posted 2006) [#6]
Well no prob. Every little bit helps I guess.