Aaron V. removed_email_address@domain.invalid wrote:

1.5

1.6

1.7

1.8

1.9

When the index numbers are float or strings, they sort like this. But I

want “1.10” to be the last number. Any idea how to put these guys in the

right order?

These are not numbers; they’re strings. Or at least, they need to be; if

they weren’t, 1.1 and 1.10 would be indistinguishable. So let’s assume

they *are* strings. Then what you’re asking to do is to compare the two

halves of each string as an integer. Take 1.1 and 2.1, for instance;

they compare as 1 and 2 would compare as integers. Very well, now take

1.10 and 1.2, for instance. 1 and 1 are the same, so 10 and 2 need to

compare as the integers 10 and 2. So:

arr = %w{

1.1

1.10

1.2

1.3

1.4

1.5

1.6

1.7

1.8

1.9

}

arr = arr.sort do |x,y|

x1, x2 = x.split(".")

y1, y2 = y.split(".")

if x1 != y1

x1.to_i <=> y1.to_i

else

x2.to_i <=> y2.to_i

end

end

p arr

If there are a lot of these, however, it would be much better to do a

keyed sort using sort_by. How to create keys depends on what you know

about the data. If we knew for a fact that the second half was always

between 1 and 99, it would be easy:

arr = arr.sort_by do |x|

x1, x2 = x.split(".")

x1.to_i * 100 + x2.to_i

end

If the second half can be any length, though, you’ll have to determine

the maximum length first, and change that “100” multiplier accordingly.

m.