sorting my array
BlitzMax Forums/BlitzMax Beginners Area/sorting my array
| ||
I have an array I want to sort depending on the points a driver has, but it's not working. I have an array of drivers objects (drivers), who have a fields for their id and their points. the drivers are put into the driverStandings array depending on the amount of points they get. the driverStandings array needs to be sorted by the driverStandings[i,2] which holds the points number. For Local i:Int = 1 To 24 driverStandings[i,0] = 0 driverStandings[i,1] = 0 Next For Local i:Int = 1 To 24 For Local j:Int = 1 To 24 driverStandings[j,0] = j 'set the id to the current position 'if the current drivers points are greater than the driver at the current position in the driverStandings array or the driverStandings array current position is blank If drivers[i].points > driverStandings[j,2] Or driverStandings[j,1] = 0 driverStandings[j,1] = drivers[i].id 'set the current position to the current driver driverStandings[j,2] = drivers[i].points 'set the current positions points to the current drivers points Exit 'exit loop Else EndIf Next Next This, works at first, but iafter the first race it seems to miss out 3 drivers. Last edited 2012 |
| ||
why are you using an int array and a type array? why don't you just use two arrays of type. both holding the same array one sorted and one original example: driverStandigs:TDriver[25] driver:Tdriver[25] For Local i:Int = 0 Until 25 driverStanding[i] = driver[i] 'both arrays hold the same data no new TDrivers are created" Next Local n:Int = 25 For Local j:Int = 0 Until 25 if n = 0 continue For Local i:Int = 0 Until n-1 If driverStanding[i].points > driverStanding[i+1].points Local temp:TDriver = driverstanding[i] driverStanding[i] = driverStanding[i+1] driverStanding[i+1] = temp EndIf Next n :- 1 Next if you want to store the previous position then just add a field to the driver and store it as you did before. Last edited 2012 |
| ||
because, the int array driverStandings, is to only display the points, the array of type holds all the drivers data, stats and info. and the array of type will have more drivers than I want in the driverStandings array, as will only add drivers who have points. |
| ||
well you can do it like this:driverStandig:TDriver[25] driver:Tdriver[25] local j:int = 0 For Local i:Int = 0 Until 25 if driver[i].points = 0 continue driverStanding[j] = driver[i] 'both arrays hold the same data no new TDrivers are created and only drivers with points are added" j :+ 1 Next Local n:Int = 25 For Local j:Int = 0 Until 25 if n = 0 continue For Local i:Int = 0 Until n-1 If driverStanding[i].points > driverStanding[i+1].points Local temp:TDriver = driverstanding[i] driverStanding[i] = driverStanding[i+1] driverStanding[i+1] = temp EndIf Next n :- 1 Next the reason I tell you to do it like this is because this way uses less memory and can be managed a bit simpler. and you are not creating extra fields you are only working with data that is already created. it s also just a suggestion as I had/am having a hard time trying to figure out what original code does and where it's messing up. Last edited 2012 |
| ||
the reason I tell you to do it like this is because this way uses less memory and can be managed a bit simpler. and you are not creating extra fields you are only working with data that is already created. ahh, I didn't know this, I thought the opposite that copying it would add to memory. I think I've got it into my thick skull. haha I reversed the if statement to this driverStanding[i].points < driverStanding[i+1].points as it was sorting them the wrong way (putting the highest points at the bottom) Thanks for your time. much appreciated. |
| ||
Array to List, sort, List to Array should also work. |