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 7012037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:12:14+00:00 2026-05-27T22:12:14+00:00

What would be the optimal solution for the following problem? I have original_string =

  • 0

What would be the optimal solution for the following problem?

I have

original_string = "This is a string that I am trying to sort"

I also have

array_to_sort = ['sort', 'string', 'This is', 'I', 'trying to', 'am', 'a'] 

I need to sort the array, so that elements are in the same order as in string. The elements are sometimes grouped together, but always in the same way as they are in string (i.e. there can be no ‘is This’ element in the array, only ‘This is’)..

All this is happening within the Rails application, so I was thinking of maybe taking the database approach and saving elements in database and then using some keys to reconstruct the original_string.. but maybe just doing some .sort trick is better.. The result does not necessarily have to be an array, can be anything..

Thanks for any input.

P.S. including an nlp tag, because this is a result of some nlp exercise.

  • 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-05-27T22:12:15+00:00Added an answer on May 27, 2026 at 10:12 pm
    array_to_sort.sort_by { |substr| original_string.index(substr) }
    

    The result is a new array, sorted by the position of the substring in the original string.

    If you want to sort in-place (by changing the original array), you can use the sort_by! method instead.

    Obviously, it’s too stupid to detect doubles (i.e. "I am what I am", ["I am", "I am", "what"] will not be sorted as one hopes).

    EDIT Making it not quite so stupid is not quite so trivial:

    def get_all_positions(str, substr)                                                                                                                                                                                           
      pattern = Regexp.new('\b' + Regexp::escape(substr) + '\b')
      result = []
      pos = -1
      while match = pattern.match(str, pos + 1)
        pos = match.offset(0)[0] + 1
        result << pos
      end
      result
    end
    
    def sort_array_according_to_string(arr, str, i=0, positions=nil)
      positions ||= Hash.new
      if i < arr.count
        current = arr[i]
        current_positions = get_all_positions(str, current)
        result = []
        current_positions.each do |pos|
          if !positions[pos]
            positions[pos] = [pos, i, current]
            result += sort_array_according_to_string(arr, str, i + 1, positions)
            positions.delete(pos)
          end
        end
      else
        sorted = positions
          .values
          .sort_by { |position, i| position }
          .map { |position, i| arr[i] }
        result = [sorted]
      end
      if i == 0
        result.uniq!
      end
      result
    end
    
    original_string = 'this is what this is not'
    example_array = ['this', 'is', 'is not', 'what', 'this']
    solution = sort_array_according_to_string(example_array, original_string)
    puts solution.inspect
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to find the optimal solution to the following (interesting?) problem that
I'm wondering if there is an optimal solution for this problem: I have a
This is possibly a problem with possibly no optimal solution. Suppose I have a
Would the following SQL remove also the index - or does it have to
I am looking for shortest solution for the following problem: I would like to
I would like to know the most optimal/elegant solution of doing this. Basically i
I'm looking for an elegant, high performance solution to the following problem. There are
I have a tricky problem that I've been messing about with for a few
I am thinking about this topcoder problem . Given a string of digits, find
I have a very large Form with many date fields that need to be

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.