- Home /
Trying to sort an array. What's wrong here?
Okay, it's late and I'm tired and it's probably something REALLY simple but I'm trying to sort this array. Basically, I want to set the split times in order and I can't see why the following code isn't working, but it's not...
function SortSplitTimes()
{
for (var i = 0; i < (AIDriverArray.length - 1); i++)
{
for (var j = (i + 1); j < AIDriverArray.length; j++)
{
if (AIDriverArray[i].fSplitTime < AIDriverArray[j].fSplitTime)
{
fTemp = AIDriverArray[i].fSplitTime;
AIDriverArray[i].fSplitTime = AIDriverArray[j].fSplitTime;
AIDriverArray[j].fSplitTime = fTemp;
}
}
print(AIDriverArray[i].fSplitTime);
}
}
Help greatly appreciated!
Just a point - didn't you fancy using .Sort on the array?
Array.Sort(AIDriverArray, function(a,b) a.fSplitTime < b.fSplitTime ? -1 : 1);
The problem with your existing code is that it's changing the split time for the drivers (not ordering the drivers themselves!!!)
I'm not using the Array class... will that matter? I guess I could give it a go. Nearly 2am... TI$$anonymous$$E FOR $$anonymous$$ORE COFFEE!!!!
Not if AIDriverArray is an array - if it's a list then you can do:
AIDriverArray.Sort(function(a,b) a.fSplitTime < b.fSplitTime);
If you manage to get the sorting function called, the issue @whydoidoit's pointed out still hits you. Your method doesn't sort the AIDrivers, only their times (example: if Schumacher was faster than Alonso, he isn't listed before him, ins$$anonymous$$d, Alonso gets Schumacher's time and Schumacher gets Alonso's time :))
Answer by POLYGAMe · Apr 07, 2013 at 03:29 AM
Okay, answered... shows how tired I was. I'd created the array of Driver cars but I hadn't populated it. HAHAHAHAHA. WHat an egg... I was up for hours, too...
Your answer
Follow this Question
Related Questions
Need recommendation for a sorting method for Enemy distances from Player (Ascending) - C# 1 Answer
How to create a table, array of array? 1 Answer
my QandA array is not working when you choose 3 wrong answers 1 Answer
Array empties values on RunTime? 1 Answer
Array with two values per element 1 Answer