How do I sort floats by highest to lowest value?

Say I have an array filled with times, but in seconds accurate down to the millisecond. How would I go about sorting these in order from highest to lowest value?

I have tried this method out:

But, it only seems to sort intergers. I need it to sort down to the decimal.

Did you try that code you linked to? I don't think it would care if it were ints or floats

Yea.. it worked, but, for some reason, it rounded everything up. One thing I tried doing is multiplying by 10000 so that the decimal places would be counted for, but it's really a pain to do it that way.

1 Answer

1

If you’re not worried about the internals of the sort, letting the .NET framework take care of it for you may save some headaches. You didn’t mention which particular data structure you’re using, so I’ll offer an example which includes some popular choices.

#pragma strict

import System.Collections.Generic;

//sort a built-in array of floats
var x = new float[3];
x[0] = 300.1;
x[1] = 200.2;
x[2] = 500.3;
System.Array.Sort(x);

//sort an Array() of floats
var y = new Array();
y.Push( 300.1 );
y.Push( 200.2 );
y.Push( 500.3 );
y.Sort();

//sort a .NET list of floats
var z = new List.<float>();
z.Add( 300.1 );
z.Add( 200.2 );
z.Add( 500.3 );
z.Sort();

//reverse our lists
System.Array.Reverse(x);
y.Reverse();
z.Reverse();