views:

33

answers:

1

Hi,

I'm using ruby to sort an array where each element in the array is another array.

I have this:

Data = Data.SortBy { |Info| info[3] }

example data in this column:

3.1
2
5.65
-1
0.4
-9.43
-10.87
-2.3

It should sort this into:

5.65
3.1
2
0.4
-1
-2.3
-9.43
-10.87

But it comes out like this:

5.65
3.1
2
0.4
-1
-10.87
-2.3
-9.43

It's only comparing the first char of the float... not the whole number?

+1  A: 

Looks like it's sorting alphabetically instead of numerically, that's why "1" comes before "10".

I assume the data in info are strings. If so, you can convert them to numerical floats for the sort operation:

data = data.sort_by{|info| info[3].to_f}
Roadmaster
This worked great, I should of been able to get this on my own. Long day :)
Steven
Then accept the answer, there's a checkmark just for that purpose.
Marc-André Lafortune
I have... It's not unhuman to also give a personal thks. Maybe for you though... It didn't let me do it within 5 mins as well..
Steven
Troll much LOLOLOLOLOL kthksbye
Steven