Problem with the .sort method

I’ve got an array that’s constructed like this: array = [[a1,[b1,c1,d1]], [a2,[b2,c2,d2]], [a3,[b3,c3,d3]], [a4,[b4,c4,d4], …]
‘a’ is a floating point, ‘b’ is a Point3d and ‘c’ and ‘d’ is a string.

I want to sort this array for ‘a’, so I use array.sort. This works fine unless two values of ‘a’ are equal. In two values for ‘a’ are equal the sort function will sort on the next element of the array which is ‘b’. These are Point3d’s so they can’t get sorted.

I found out about the .sort_by method which is implemented from ruby 1.8.7, but I’m using SketchUp 8, so I’m restricted to ruby 1.8.6. Does anyone have another solution?

You can pass a block to Enumerable#sort that implements whatever comparison logic you want:

enum.sort {|a,b| …}

The block must return 1, 0, or -1, in keeping with the native <=> operator.

I’ve been able to make it work with your information.

Thanks Steve

For the record:

  ary.sort {|a,b|
    float_sort = a[0] <=> b[0]
    next float_sort unless float_sort == 0
    a[1][0].to_a <=> b[1][0].to_a
1 Like