Skip to content

Commit

Permalink
Merge pull request #152 from samvera/num_sort
Browse files Browse the repository at this point in the history
add ability to sort based on a numeric ranking predicate
  • Loading branch information
elrayle authored Jul 18, 2018
2 parents 5b62c9d + e8bdfef commit 0327912
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
17 changes: 16 additions & 1 deletion lib/qa/authorities/linked_data/search_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ def sort_search_results(json_results) # rubocop:disable Metrics/MethodLength
return json_results unless supports_sort?
json_results.sort! do |a, b|
cmp = sort_when_missing_sort_predicate(a, b)
next unless cmp.nil?
next cmp unless cmp.nil?

cmp = numeric_sort(a, b)
next cmp unless cmp.nil?

as = a[:sort].collect(&:downcase)
bs = b[:sort].collect(&:downcase)
Expand Down Expand Up @@ -139,6 +142,18 @@ def sort_when_same_but_one_has_more_values(as, bs, current_list_size)
return 1 if bs.size <= current_list_size # consider shorter b list of values lower then longer a list
nil
end

def numeric_sort(a, b)
return nil if a[:sort].size > 1
return nil if b[:sort].size > 1
return nil unless s_is_i? a[:sort][0]
return nil unless s_is_i? b[:sort][0]
Integer(a[:sort][0]) <=> Integer(b[:sort][0])
end

def s_is_i?(s)
/\A[-+]?\d+\z/ === s # rubocop:disable Style/CaseEquality
end
end
end
end
38 changes: 33 additions & 5 deletions spec/lib/authorities/linked_data/search_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
context 'when sort predicate is specified in configuration' do
let(:auth_name) { :LOD_SORT }

context "when sort term is empty" do
context "and sort term is empty" do
context "for all" do
it "does not change order" do
json_results = [{ label: "[#{term_b}]", sort: [""] },
Expand All @@ -35,19 +35,39 @@
end

context "for one" do
it "puts empty first" do
json_results = [{ label: "[#{term_b}]", sort: [""] },
it "puts empty first when empty is in 1st position" do
json_results = [{ label: "['_empty_1_']", sort: [""] },
{ label: "[#{term_c}]", sort: [term_c] },
{ label: "[#{term_a}]", sort: [term_a] }]
expected_results = [{ label: "[#{term_b}]" },
expected_results = [{ label: "['_empty_1_']" },
{ label: "[#{term_a}]" },
{ label: "[#{term_c}]" }]
expect(instance.send(:sort_search_results, json_results)).to eq expected_results
end

it "puts empty first when empty is in 2nd position" do
json_results = [{ label: "[#{term_b}]", sort: [term_b] },
{ label: "['_empty_2_']", sort: [""] },
{ label: "[#{term_a}]", sort: [term_a] }]
expected_results = [{ label: "['_empty_2_']" },
{ label: "[#{term_a}]" },
{ label: "[#{term_b}]" }]
expect(instance.send(:sort_search_results, json_results)).to eq expected_results
end

it "puts empty first when empty is in last position" do
json_results = [{ label: "[#{term_b}]", sort: [term_b] },
{ label: "[#{term_c}]", sort: [term_c] },
{ label: "['_empty_last_']", sort: [""] }]
expected_results = [{ label: "['_empty_last_']" },
{ label: "[#{term_b}]" },
{ label: "[#{term_c}]" }]
expect(instance.send(:sort_search_results, json_results)).to eq expected_results
end
end
end

context "when sort term is single value" do
context "and sort term is single value" do
context "for all" do
it "sorts on the single value" do
json_results = [{ label: "[#{term_b}]", sort: [term_b] },
Expand Down Expand Up @@ -98,6 +118,14 @@
end
end
end

context "and sort values are numeric" do
it "does numeric compare" do
json_results = [{ label: "['22']", sort: ["22"] }, { label: "['1']", sort: ["1"] }, { label: "['215']", sort: ["215"] }]
expected_results = [{ label: "['1']" }, { label: "['22']" }, { label: "['215']" }]
expect(instance.send(:sort_search_results, json_results)).to eq expected_results
end
end
end
end
end

0 comments on commit 0327912

Please sign in to comment.