Re: stable sort_by?


#1

I guess you have to use sort instead of sort_by in such a case,
like:

test = [[1, ‘b’], [1, ‘c’], [1, ‘a’], [0, ‘a’]]

#sort for first item, than for second in reverse order
p test.sort{|a, b|(a[0] <=> b[0]).nonzero? || (b[1] <=> a[1]) }

=>[[0, “a”], [1, “c”], [1, “b”], [1, “a”]]

cheers

Simon


#2

Kroeger, Simon (ext) wrote:

I guess you have to use sort instead of sort_by in such a case,
like:

test = [[1, ‘b’], [1, ‘c’], [1, ‘a’], [0, ‘a’]]

#sort for first item, than for second in reverse order
p test.sort{|a, b|(a[0] <=> b[0]).nonzero? || (b[1] <=> a[1]) }

=>[[0, “a”], [1, “c”], [1, “b”], [1, “a”]]

Aaah, it’s chaining with nonzero? (since it’ll return the -1 or 1 if
that’s the case) that I was missing.

Thanks.