0

I have an array of songs that contains:

name artist album genre

The array is completely random at the moment.

Aim

I want the user to be able to view the contents separately by name, or by artist etc..

Attempt

I recall, in another language (php) I could create a new array of, for example, artists and then sort the songs array by the artists. This is good in theory but how to implement?

I created a sorted array of artists, like this:

    songs.each { |member|
               artists << member.artist
      }

artists.sort!

This works great, and outputs all the artists to the screen alphabetically when testing.

My problem is how would I sort the songs array by the artists array? I.e. the songs would be sorted by the artists

Question

Is there an easier way to sort the songs array alphabetically?

Ultimately, it would be ideal to have an name array, artist array, album array, genre array, each of which would have the same contents as the songs array, but sorted alphabetically by the relevant field.

Thanks for any help.

Example

Input:

Songs =

[['Song A','B Artist'],['Song D','A Artist'],['Song D','C Artist']]

Example Output

Artist =

[['A Artist','Song D'],['B Artist','Song A'],['C Artist','Song D']]

3
  • 1
    can you give an example please: input array and output array. Commented Oct 16, 2013 at 10:31
  • @sahildhankhar example added.. Commented Oct 16, 2013 at 10:41
  • songs.sort_by {|song| song[0]} or songs.sort_by {|song| song[1]} whatever index of the song you want to sort on. works for you? Commented Oct 16, 2013 at 10:53

1 Answer 1

2

There's Enumerable#sort_by:

songs = [
  {name: "Like a Rolling Stone", artist: "Bob Dylan"},
  {name: "(I Can't Get No) Satisfaction", artist: "The Rolling Stones"},
  {name: "Imagine", artist: "John Lennon" }
]

songs.sort_by { |song| song[:name] }
#=> [
#     {:name=>"(I Can't Get No) Satisfaction", :artist=>"The Rolling Stones"},
#     {:name=>"Imagine", :artist=>"John Lennon"},
#     {:name=>"Like a Rolling Stone", :artist=>"Bob Dylan"}
#   ]

songs.sort_by { |song| song[:artist] }
#=> [
#     {:name=>"Like a Rolling Stone", :artist=>"Bob Dylan"},
#     {:name=>"Imagine", :artist=>"John Lennon"},
#     {:name=>"(I Can't Get No) Satisfaction", :artist=>"The Rolling Stones"}
#   ]
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks .. interesting answer, Although I have no idea how I would implement this - very much a novice
Please be more specific, where are you stuck?
I attempted to sort my array using songs.sort_by { |song| song[:artist] } as you suggested, and I get this error: undefined method '[]'. However - I suspect that your answer requires me to build my array in a different manner - is that right? I am not familiar with enumerable array
I should rephrase that - How would I create the array so that I could used the Enumerable Sort by? My songs array is 1876 elements long!
Not sure if I understand your problem. sort_by should work fine even with much larger arrays.

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.