Module: Sorting

Defined in:
lib/sorting.rb,
lib/sorting/bigger.rb,
lib/sorting/helpers.rb,
lib/sorting/smaller.rb,
lib/sorting/version.rb,
lib/sorting/ascending.rb,
lib/sorting/descending.rb

Overview

Sorting Helpful functionality for sorting and comparing.

Requiring 'sorting' loads all functionality this gem provides, except for the patches on Kernel. For those you must require 'sorting/convenience'.

Examples:

Convenient

require 'sorting/convenience'
people.sort_by { |person| [asc(person.first_name), asc(person.last_name), desc(person.age)] }

Or without patching Kernel

require 'sorting'
people.sort_by { |person|
  [Sorting.asc(person.first_name), Sorting.asc(person.last_name), Sorting.desc(person.age)]
}

Care about expensive comparison values which may not always be needed

# assume item.expensive_value takes a lot of time to compute, but since it's the second value,
# it might only be needed in a small number of cases.
require 'sorting/convenience'
items.sort_by { |item|
  [asc(item.cheap_value), asc(:nils_last) { item.expensive_value }]
}

Care about nil values in your data

require 'sorting/convenience'
people.sort_by { |person|
  [asc(person.first_name, :nils_first), asc(person.first_name, :nils_last)]
}

Only care about nil values in your data

[5,3,nil,9].sort_by { |x| x || Sorting::Bigger } # Sorting::Smaller is available too

Defined Under Namespace

Modules: Bigger, Helpers, Smaller Classes: Ascending, Descending

Constant Summary collapse

Version =
Note:

require 'sorting' loads this module

The version of the sorting gem.

Gem::Version.new("0.0.2")

Class Method Summary collapse

Class Method Details

.asc(*args, &lazy) ⇒ Object

Note:

This method is a module function, and as such also available as a private instance method.


# File 'lib/sorting.rb', line 50

.desc(*args, &lazy) ⇒ Object

Note:

This method is a module function, and as such also available as a private instance method.


# File 'lib/sorting.rb', line 58