Your question needs a bit of clarification--please use a examples of
the query, the data set format to be searched, and the expected
result. And then repeat for the case that you are having trouble
with.
But I'll throw out some thoughts even though I am confused about what
you are trying to do...
Here is an example of how to sort_by last_name on an array of hashes
with first_name and last_name keys .
array.sort_by{|hash| hash[:last_name]}
irb
test = [{:first_name=>'tim', :last_name=>'rand'},{:first_name=>'jim', :last_name=>'band'},{:first_name=>'him', :last_name=>'crand'}]
=> [{:first_name=>"tim", :last_name=>"rand"},
{:first_name=>"jim", :last_name=>"band"},
{:first_name=>"him", :last_name=>"crand"}]
test
=> [{:first_name=>"tim", :last_name=>"rand"},
{:first_name=>"jim", :last_name=>"band"},
{:first_name=>"him", :last_name=>"crand"}]
test.sort_by{|hash| hash[:first_name]}
=> [{:first_name=>"him", :last_name=>"crand"},
{:first_name=>"jim", :last_name=>"band"},
{:first_name=>"tim", :last_name=>"rand"}]
test.sort_by{|hash| hash[:last_name]}
=> [{:first_name=>"jim", :last_name=>"band"},
{:first_name=>"him", :last_name=>"crand"},
{:first_name=>"tim", :last_name=>"rand"}]
To find misspelled names is a bit trickier--I would probably use the
text rubygem as it has the ability to calculate the Levenshtein
distance (basically number of substitutions, deletions and insertions)
required to spell the target using a query. You would have to compare
the query to all names and sort based on the levenshtein distance and
then pull the closest match.I have used that strategy in the past and
it works. Here is a quick demo of the syntax for the levenshtein
distance:
irb
require 'text'
=> true
Text::Levenshtein.distance('this', 'that')
=> 2
Text::Levenshtein.distance('query', 'queen')
=> 2
To the extent that I think I understand your question, I bet having
some verification is going to be unavoidable. Something like the
following to catch cases when people type in a space separated first
and last name.
if query.match(" ") #query is something like "first last"
query_first, query_last = "first last".split(/ /)[0], "first last".split(/ /)[1]
else
query_first = query_last = query
end
Hope that helps,
Tim