Skip to main content
deleted 143 characters in body
Source Link
Nakilon
  • 1.3k
  • 7
  • 18

Using magical -1 is a mistake from imperative programming, because tomorrow those incoming numbersYou can happen to be negative. Correct solution is to create temporary sorting columns:

def sort records, *attrs
  records.sort_by do |h|
    h.values_at(*attrs).map do |v|
      v.nil? ? [2] : [1, v]
    end
  end
end

Here I added columns with values 1 or 2 to the left for higher priority -- could add to the right or even in between for more complex sorting.

Using magical -1 is a mistake from imperative programming, because tomorrow those incoming numbers can happen to be negative. Correct solution is to create temporary sorting columns:

def sort records, *attrs
  records.sort_by do |h|
    h.values_at(*attrs).map do |v|
      v.nil? ? [2] : [1, v]
    end
  end
end

Here I added columns with values 1 or 2 to the left for higher priority -- could add to the right or even in between for more complex sorting.

You can create temporary sorting columns:

def sort records, *attrs
  records.sort_by do |h|
    h.values_at(*attrs).map do |v|
      v.nil? ? [2] : [1, v]
    end
  end
end

Here I added columns with values 1 or 2 to the left for higher priority -- could add to the right or even in between for more complex sorting.

Source Link
Nakilon
  • 1.3k
  • 7
  • 18

Using magical -1 is a mistake from imperative programming, because tomorrow those incoming numbers can happen to be negative. Correct solution is to create temporary sorting columns:

def sort records, *attrs
  records.sort_by do |h|
    h.values_at(*attrs).map do |v|
      v.nil? ? [2] : [1, v]
    end
  end
end

Here I added columns with values 1 or 2 to the left for higher priority -- could add to the right or even in between for more complex sorting.