Last Updated
Viewed 12 Times

I want to change the starting and ending items in my array without changing the order of my items, if that makes sense. Here is my order:

order = ['B', 'E', 'A', 'D', 'G', 'C', 'F']

I would like to sort this array in a way that would return as follows:

['F', 'B', 'E', 'A', 'D', 'G', 'C']
['C', 'F', 'B', 'E', 'A', 'D', 'G']

In other words, 'B' should always precede'E', 'E' should always precede'A', and so on, unless the new array starts with 'E' for example, then 'B' should move to the end of the array.

I have three arrays.

My main list contains a mix of different entities which are verified in a DB:

ab = ["a:555", "b:222", "a:333", "b:777", "a:777", "a:999", "b:111"]

I have two more arrays of a and b entities separated, but ordered (some are missing):

# notice that some of the items from the initial list are missing, but the order is preserved!
a = [{id}, "a:777", "a:999"]
b = ["b:222", "b:111"]

What is an efficient way to merge a and b in array c preserving the order in ab where the items are present? My expected result from the procedure is:

c = ["a:555", "b:222", "a:777", "a:999", "b:111"]

I'm a Ruby newbie and everything I came up with is utterly ugly.


I did know it matters, and would be confusing, but a and b are complex objects (AR) that represent the strings in ab. To clarify with my code:

ab = ["a:555", "b:222", "a:333", "b:777", "a:777", "a:999", "b:111"]
a = [{:id => 555}, {:id => 777}, {:id => 999}]
b = [{:id => 222}, {:id => 111}]
c = []

ab.each { |item|
parts = item.split(":")
if parts[0] == "a"
  if a[0][:id].to_s() == parts[1]
    c << a.shift()
  if b[0][:id].to_s() == parts[1]
    c << b.shift()  


puts c

I have two arrays a, b of the same length:

a = [a_1, a_2, ..., a_n]
b = [b_1, b_2, ..., b_n]

When I sort a using sort_by!, the elements of a will be arranged in different order:

a.sort_by!{|a_i| some_condition(a_i)}

How can I reorder b in the same order/rearrangement as the reordering of a? For example, if a after sort_by! is

[a_3, a_6, a_1, ..., a_i_n]

then I want

[b_3, b_6, b_1, ..., b_i_n]


I need to do it in place (i.e., retain the object_id of a, b). The two answers given so far is useful in that, given the sorted arrays:


I can do


but if possible, I want to do it directly. If not, I will accept one of the answers already given.

Similar Question 3 : How can I sort this array?

I have an array, headlines, that holds several sentences, so like:

headlines = ["I see a tree", "Facebook is slow", "plants need water to grow", "There's an orange", "I think we'll agree"] 
first = headlines[0]
second = headlines[1]
third = headlines[2]

I am using the ruby_rhymes gem which provides a method #to_phrase.rhymes which prints out rhyming words for the last word in a string you provide it with. Now to check if the array strings rhyme, I do something like:

> first.to_phrase.rhymes.flatten.join(", ").include?(second.to_phrase.rhymes.flatten.join(", "))
=> false
> second.to_phrase.rhymes.flatten.join(", ").include?(third.to_phrase.rhymes.flatten.join(", "))
=> true

I want to save these to a text file so I want to sort them in the array so that rhyming pairs are subsequent to one another. I know to sort so that strings follow if the last 3 characters are the same is:

headlines.sort! {|a,b|  a[-3,3] <=> b[-3,3] }

But I don't know how to do want I want.

Similar Question 4 (3 solutions) : How to sort an array in Ruby to a particular order?

Similar Question 5 (1 solutions) : How to sort array in descending order [duplicate]