Class: Sorting::Descending

Inherits:
Ascending show all
Defined in:
lib/sorting/descending.rb

Overview

Note:

require 'sorting' loads this class

This class is used to implement 'desc' (descending) sorting. You should not need to use this class directly. You can require 'sorting/convenience' and then use Kernel#desc, or you can require 'sorting' and then use Sorting#desc (which is a module_function, and hence available as Sorting::desc)

Constant Summary

Constants inherited from Ascending

Ascending::NilsValues

Instance Attribute Summary

Attributes inherited from Ascending

#treat_nils

Instance Method Summary collapse

Methods inherited from Ascending

#initialize, #nils_first?, #nils_last?, #value

Constructor Details

This class inherits a constructor from Sorting::Ascending

Instance Method Details

#<=>(other) ⇒ -1, ...


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sorting/descending.rb', line 43

def <=>(other)
  my_value = value

  if @treat_nils && my_value.nil?
    @treat_nils == :nils_first ? -1 : 1
  else
    other_value = other.value

    if @treat_nils && other_value.nil?
      @treat_nils == :nils_first ? 1 : -1
    else
      other_value <=> my_value
    end
  end
end