Skip to content

Commit bb0317e

Browse files
authored
Merge pull request #2015 from codidact/0valt/2009/tag-sorting
2 parents 4deef91 + b90b353 commit bb0317e

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

app/models/tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def self.search(term)
4949

5050
# Select from the union of the above queries, select only the tag columns such that we can distinct them
5151
from(Arel.sql("((#{q1.to_sql}) UNION (#{q2.to_sql})) tags"))
52-
.order(Arel.sql(sanitize_sql_array(['name LIKE ? DESC, name', value])))
52+
.order(Arel.sql(sanitize_sql_array(['name LIKE ? DESC, char_length(name), name', value])))
5353
.select(Tag.column_names.map { |c| "tags.#{c}" })
5454
.distinct
5555
end

test/models/tag_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TagTest < ActiveSupport::TestCase
1212

1313
tags = Tag.search(term).to_a
1414

15-
name_match_sorted = tags.select { |t| t.name.include?(term) }.sort { |a, b| a.name <=> b.name }
15+
name_match_sorted = tags.select { |t| t.name.include?(term) }.sort { |a, b| a.name.length <=> b.name.length }
1616
excerpt_match_sorted = tags.select { |t| t.excerpt&.include?(term) }.sort { |a, b| a.name <=> b.name }
1717
sorted_tags = name_match_sorted + excerpt_match_sorted
1818

0 commit comments

Comments
 (0)