Hi,
Here’s an example of an array I want to sort:
ary = [[0.2, “b”], [0.4, “b”], [0.2, “c”], [0.0, “c”], [0.5, “a”], [0.7,
“d”], [0.7, “a”]]
The array should be sorted by the first element of every ‘subarray’. I
do this with ary.sort_by{|element|element[0]} method. This works fine,
but when two values are equal (which happens twice in this example) I
need an extra condition to sort these equal values.
The sorted array should look like:
[[0.0, “c”], [0.2, “c”], [0.2, “b”], [0.4, “b”], [0.5, “a”], [0.7, “a”],
[0.7, “d”]]
For the case of 0.7: The arrays that contain as second element “a” and
“d”. There’s another array with “a”, but no other array with element
“d”. This means that the array of “a” should be sorted before “d”.
For the case of 0.2: The second element of these arrays are “c” and “d”.
These second element both occur in another array. The value of these
second array should decide how the sorting goes. So “b” before “c”.
I’m having trouble figuring this out. I hope I’ve been clear.