Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7905343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:26:37+00:00 2026-06-03T10:26:37+00:00

How can improve this sort method to meet the following conditions: exact matches returned

  • 0

How can improve this sort method to meet the following conditions:

  • exact matches returned first
  • partial matches follow exact matches
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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:26:38+00:00Added an answer on June 3, 2026 at 10:26 am

    You could make a note of which ones are exact matches and which are full matches:

    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
    

    and then sort on both values and unpack the internal arrays:

    matches.sort.map(&:last)
    

    I’m not sure what you’re expecting this sort to do:

    sorted.sort! {|b| term <=> b }
    

    but it is going to do strange things because the sort block is supposed to compare two elements of the array with each other and you’re completely ignoring the second one; for example, this happens for me:

    >> [4,2,1,2,4].sort { |x| 3 <=> x }
    => [4, 4, 1, 2, 2]
    

    and the resultant ordering doesn’t make much sense.


    The each_with_object is doing several things at once:

    1. Find the exact matches and mark them as exact matches (the leading 0).
    2. Find the prefix matches and mark them as partial matches (the leading 1).
    3. Return the combined list to be stored in matches; e.each_with_object(m) hands m to the block as its second argument and returns m.

    This leaves you with a matches that looks like this:

    [ [0, a], [1, b], [1, c], ... ]
    

    with a leading 0 indicating an exact match and 1 indicating a prefix match. Then you can let sort sort matches normally since Array#<=> compares arrays element by element; 0 comes before 1 so the exact matches end up being first. Then we can throw away the exact/partial indicators using map to call last on each of the inner arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I improve this username/password checking? [AcceptVerbs(HttpVerbs.Post)] public ActionResult Login(FormCollection collection) { var
How can I improve this code? What has made this long winded is the
I have read that you can do it, but would this really improve performance
Can the following code cause a memory leak? Would using a StringBuffer actually improve
I have an idea of how I can improve the performance with dynamic code
I wonder if anyone can help improve my understanding of JOINs in SQL. [If
I was wondering what settings an application can control to improve battery life in
What kind of trick can I use to speed up and improve an image
If I use String.intern() to improve performance as I can use == to compare
Using dotnet 2.0. Can the following code be improved in style ? private object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.