Class: Qa::LinkedData::DeepSortService

Inherits:
Object
  • Object
show all
Defined in:
app/services/qa/linked_data/deep_sort_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(the_array, sort_key, preferred_language = nil) ⇒ Object

Returns instance of this class.

Examples:

the_array parameter

[
  {:uri=>[#<RDF::URI:0x3fcff54a829c URI:http://id.loc.gov/authorities/names/n2010043281>],
   :id=>[#<RDF::Literal:0x3fcff4a367b4("n 2010043281")>],
   :label=>[#<RDF::Literal:0x3fcff54a9a98("Valli, Sabrina"@en)>],
   :altlabel=>[],
   :sort=>[#<RDF::Literal:0x3fcff54b4c18("2")>]},
  {:uri=>[#<RDF::URI:0x3fcff54a829c URI:http://id.loc.gov/authorities/names/n201002344>],
   :id=>[#<RDF::Literal:0x3fcff4a367b4("n 201002344")>],
   :label=>[#<RDF::Literal:0x3fcff54a9a98("Cornell, Joseph"@en)>],
   :altlabel=>[],
   :sort=>[#<RDF::Literal:0x3fcff54b4c18("1")>]}
]

Parameters:

  • the (Array<Hash<Symbol,Array<RDF::Literal>>>)

    array of hashes to sort

  • the (sort_key)

    key in the hash on whose value the array will be sorted

  • preferred (Symbol)

    language to appear first in the list; defaults to no preference



22
23
24
# File 'app/services/qa/linked_data/deep_sort_service.rb', line 22

def initialize(the_array, sort_key, preferred_language = nil)
  @sortable_elements = the_array.map { |element| DeepSortElement.new(element, sort_key, preferred_language) }
end

Instance Method Details

#sortObject

Sort an array of hash on the specified sort key. The value in the hash at sort key is expected to be an array with one or more values that are RDF::Literals that translate to a number (e.g. 2), a string number (e.g. “3”), a string (e.g. “hello”), or a language qualified string (e.g. “hello”@en). The sort occurs in the following precedence.

  • preference for numeric sort (if only one value each and both are integers or a string that can be converted to an integer)

  • single value sort (if only one value each and at least one is not an integer)

  • multiple values sort (if either has multiple values)

Examples:

returned sorted array

[
  {:uri=>[#<RDF::URI:0x3fcff54a829c URI:http://id.loc.gov/authorities/names/n201002344>],
   :id=>[#<RDF::Literal:0x3fcff4a367b4("n 201002344")>],
   :label=>[#<RDF::Literal:0x3fcff54a9a98("Cornell, Joseph"@en)>],
   :altlabel=>[],
   :sort=>[#<RDF::Literal:0x3fcff54b4c18("1")>]},
  {:uri=>[#<RDF::URI:0x3fcff54a829c URI:http://id.loc.gov/authorities/names/n2010043281>],
   :id=>[#<RDF::Literal:0x3fcff4a367b4("n 2010043281")>],
   :label=>[#<RDF::Literal:0x3fcff54a9a98("Valli, Sabrina"@en)>],
   :altlabel=>[],
   :sort=>[#<RDF::Literal:0x3fcff54b4c18("2")>]}
]

Returns:

  • the sorted array



47
48
49
# File 'app/services/qa/linked_data/deep_sort_service.rb', line 47

def sort
  @sortable_elements.sort.map(&:element)
end