Module: CiteProc::Ruby::SortItems
- Included in:
- Engine
- Defined in:
- lib/citeproc/ruby/sort.rb
Instance Method Summary collapse
- #compare_items(a, b) ⇒ Object
- #compare_items_by_key(a, b, key) ⇒ Object
- #compare_items_by_keys(a, b, keys) ⇒ Object
- #sort!(items, keys) ⇒ Object
- #sort_case_sensitively? ⇒ Boolean
Instance Method Details
#compare_items(a, b) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/citeproc/ruby/sort.rb', line 66 def compare_items(a, b) if sort_case_sensitively? a <=> b else a.to_s.downcase <=> b.to_s.downcase end end |
#compare_items_by_key(a, b, key) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/citeproc/ruby/sort.rb', line 34 def compare_items_by_key(a, b, key) if key.macro? result = compare_items(*renderer.render_sort(a, b, key.macro, key)) else va, vb = a[key.variable], b[key.variable] # Return early if one side is nil. In this # case ascending/descending is irrelevant! return 1 if va.nil? || va.empty? return -1 if vb.nil? || vb.empty? result = case CiteProc::Variable.types[key.variable] when :names node = CSL::Style::Name.new(key.) node.all_names_as_sort_order! compare_items(*renderer.render_sort(va, vb, node, key)) when :date va <=> vb when :number va <=> vb else compare_items(va, vb) end end result = -result unless key.ascending? result end |
#compare_items_by_keys(a, b, keys) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/citeproc/ruby/sort.rb', line 22 def compare_items_by_keys(a, b, keys) result = 0 keys.each do |key| result = compare_items_by_key(a, b, key) return result unless result.zero? end result end |
#sort!(items, keys) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/citeproc/ruby/sort.rb', line 6 def sort!(items, keys) return items unless !keys.nil? && !keys.empty? # TODO refactor if items.is_a?(CitationData) items.sort! do |a, b| compare_items_by_keys(a.data, b.data, keys) end else items.sort! do |a, b| compare_items_by_keys(a, b, keys) end end end |
#sort_case_sensitively? ⇒ Boolean
74 75 76 77 |
# File 'lib/citeproc/ruby/sort.rb', line 74 def sort_case_sensitively? return false unless processor && processor. processor.[:sort_case_sensitively] end |