Module: DeepSort::DeepSortHash
- Defined in:
- lib/deepsort.rb
Overview
inject this method into the Hash class to add deep sort functionality to Hashes
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
comparison for hashes is ill-defined.
- #deep_sort ⇒ Object
- #deep_sort! ⇒ Object
- #deep_sort_by(&block) ⇒ Object
- #deep_sort_by!(&block) ⇒ Object
Instance Method Details
#<=>(other) ⇒ Object
comparison for hashes is ill-defined. this performs array or string comparison if the normal comparison fails.
93 94 95 |
# File 'lib/deepsort.rb', line 93 def <=>(other) super(other) || to_a <=> other.to_a || to_s <=> other.to_s end |
#deep_sort ⇒ Object
50 51 52 |
# File 'lib/deepsort.rb', line 50 def deep_sort deep_sort_by { |obj| obj } end |
#deep_sort! ⇒ Object
54 55 56 |
# File 'lib/deepsort.rb', line 54 def deep_sort! deep_sort_by! { |obj| obj } end |
#deep_sort_by(&block) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/deepsort.rb', line 58 def deep_sort_by(&block) Hash[self.map do |key, value| [if key.respond_to? :deep_sort_by key.deep_sort_by(&block) else key end, if value.respond_to? :deep_sort_by value.deep_sort_by(&block) else value end] end.sort_by(&block)] end |
#deep_sort_by!(&block) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/deepsort.rb', line 75 def deep_sort_by!(&block) replace(Hash[self.map do |key, value| [if key.respond_to? :deep_sort_by! key.deep_sort_by!(&block) else key end, if value.respond_to? :deep_sort_by! value.deep_sort_by!(&block) else value end] end.sort_by(&block)]) end |