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
On May 23, 4:49 pm, Robert Scott <[email protected]>
wrote:
> I have an array of hashes that contains several fields, including
> first_name and last_name. Unfortunately, since its the result of an API
> call, I have no other ways to work with it. Regardless, I'm trying to
> build a basic search function where a user can enter a name and it will
> display the results from a newly created array.
>
> I'm guessing that sort_by will be the best route to go, but I've been
> unsuccessful in finding out how to use it with multiple fields. Any
> guesses?
>
> The second part to the question is how you structure the sort_by, if
> that is the best way, to find objects that are similar to the requested
> query. It's not so much that a user would mispell a name (although that
> would be helpful) but if they put in a firstname + lastname pair, it
> wouldn't technically match with either field on its own.
>
> Thanks in advance. :)
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---