如何改进此排序方法以满足以下条件:
def find_me
records = ["gogol", "garrison", "feathers", "grains"]
sorted = []
print "what are you looking for? "
term = gets.chomp.downcase
records.select do |record|
if term == record.downcase
#exact match
sorted << record
elsif term[0] == record[0] or term[1] == record[1] or term[2] == record[2]
#if the first three chars match add it
sorted << record
end
end
sorted.sort! {|b| term <=> b }
end
答案 0 :(得分:2)
def find_me
records = ["gogol", "garrison", "feathers", "grains"]
exact_matches = []
partial_matches = []
print "what are you looking for? "
term = gets.chomp.downcase
records.each do |record|
if term == record.downcase
#exact match
exact_matches << record
elsif term.slice(0, 3) == record.slice(0, 3)
#if the first three chars match add it
partial_matches << record
end
end
# Just add the Arrays and it'll put the exact matches first and the
# partial ones last without a need for sorting. =)
sorted = exact_matches + partial_matches
end
答案 1 :(得分:1)
您可以记下哪些是完全匹配,哪些是完整匹配:
matches = records.each_with_object([]) do |record, m|
if term == record.downcase
m << [ 0, record ]
elsif term[0, 3] == record[0, 3]
m << [ 1, record ]
end
end
然后对两个值进行排序并解压缩内部数组:
matches.sort.map(&:last)
我不确定你期望这样做:
sorted.sort! {|b| term <=> b }
但它会因为sort
块应该阵列的两个元件相互比较做奇怪的事情和你&#39;再完全无视第二个;例如,这发生在我身上:
>> [4,2,1,2,4].sort { |x| 3 <=> x }
=> [4, 4, 1, 2, 2]
并且由此产生的排序没有多大意义。
each_with_object
一次做几件事: