Sorting Types

Blitz3D Forums/Blitz3D Programming/Sorting Types

Craig H. Nisbet(Posted 2004) [#1]
Anyone know how to do this? I need to sort a type list by a integer number in the type. Actually its for a purchase list in a strategy game. I'd like the lowest priced items to show up first in the list and the more expensive down the line. I'm sure this is pretty easy, I've just never written a sorting program before.


Jeremy Alessi(Posted 2004) [#2]
	For rp.richestplayer = Each richestplayer
		For rpb.richestplayer = Each richestplayer
			If rpb\money < rp\money
				tempname$ = rp\name$
				tempmoney = rp\money
				rp\name$ = rpb\name$
				rp\money = rpb\money
				rpb\name$ = tempname$
				rpb\money = tempmoney
			EndIf
		Next
	Next


Seems to work for me!


AntonyWells(Posted 2004) [#3]
blitz actually has funcs to do list 'editing', so you can literally just move them without having to copy contents etc.

repeat
   redo=false
   for play.player each player
       start.player =after play
       if start=null exit
       repeat
            if start\value<play\value
                  insert start before play ;Will re-arrenge the internal blitz list.
                  reDo=true
             endif
           start=after start
       until start =null
   next
until redo=false


is a little bubble sort way..not tested, just giving you the gist.


Jeremy Alessi(Posted 2004) [#4]
Oh yeah, I forgot about the insert commands ... I'm doing it kinda old school.


N(Posted 2004) [#5]
http://blitzbasic.com/codearcs/codearcs.php?code=1096

Look at those.

By making the array have two elements- one to hold the integer and one to hold the type handle- you can sort the array based on the integer's value and then go through the array and get the type object from the handle.

Just to note, Quicksort is the fastest sorting algorithm in that archive entry.