Trying to sort by multiple parameters but having trouble with one parameter sorting ascendingly and the other descending.
array = [20, 10, 40, 1200, 300]
I need to first sort the array by the number of digits in each number descending.
So we get [1200, 120, 40, 10, 20]
If there is a tie between lengths, I need to sort them ascendingly by value
So we get: [1200, 120, 10, 20, 40]
I'm trying to make sort_by work for this but I can't figure out how to sort the first parameter descending and the second ascendingly.
def digit_sorter(ar)
ar.sort_by {|num| [num.to_s.length, num]}.reverse
end
I've also tried [ [num.to_s.length], [num].reverse ]