0

I have an array

a = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

and another array:

b = [0, 3, 6, 3, 4, 0, 1]

Is it possible to sort array a according to values in array b?

The result should be:

a = ['c', 'e', 'b', 'd', 'g', 'a', 'f']

Something like this doesn't seem to exist in Ruby/Rails:

a.sort_with_index{ |elem, index| b[index] }

Edit: in response to the duplicate marking: the question being referred to has an array with elements having an ID, and the other array references the ID's directly. This is not the same, and the same solution will not apply...

3
  • Well, of course it's possible. It's a question of what's the cleanest, simplest way. a.sort_by {|x| -b[a.index x] } perhaps. Commented Sep 26, 2017 at 16:50
  • Another way: a.values_at(*a.each_index.sort_by { |i| -b[i] }) #=> ["c", "e", "b", "d", "g", "a", "f"]. Commented Sep 26, 2017 at 19:08
  • This is a pure-Ruby question so it should not have a Rails tag. That tag could be a small time-waster for readers only interested in Rails questions, but more importantly, readers who filter-out Rails questions might miss the opportunity to post a great answer. Commented Sep 26, 2017 at 22:36

2 Answers 2

3
a.sort_by.with_index { |_,i| [-b[i], i] }
  #=> ["c", "e", "b", "d", "g", "a", "f"]

This uses the indices of elements in a to break ties. I see from a comment on @tadman's answer that that is desired, though it is not a requirement given in the statement of the question. See the third paragraph of the doc for Array#<=> for an explanation of how arrays are ordered in sorting operations.

Sign up to request clarification or add additional context in comments.

Comments

1

You can just combine the two, sort, and strip out the original a values:

a.zip(b).sort_by { |_a, _b| -_b }.map { |_a,_| _a }

3 Comments

...or a.zip(b).sort_by(&:last).map(&:first).reverse #=> ["c", "e", "d", "b", "g", "f", "a"]
It's close, but with duplicates the original order should precede. That's why the result should be: ['c', 'e', 'b', 'd', 'g', 'a', 'f']
@rept That's a secondary sort consideration, but you didn't communicate that clearly in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.