1. ## Quick array question

Hey guys,

This may be something that's obvious, but I have a question about sorting through arrays. Let's say I have an array as follows:

Code:
`a = [ "a", "b", "b", "b", "c", "d", "d", "e" ]`
Simple enough. Now, if I wanted to sort through it by having similar values in highest amounts first (there are 4 Bs and 2 Ds as opposed to the others) so my array ends up like this:

Code:
`a = [ "b", "b", "b", "d", "d", "a", "c", "e" ]`
How would I go about doing that? Thanks.

2. Count the occurance of each of the characters and then rewrite the array.

3. I was looking for something more along the lines of a method to use, because I can count the number of items to my heart's content but I still don't know how to sort the array.

4. As a nice unreadable one-liner:

Code ruby:
```def sort_by_occurrences(xs)
xs.inject(Hash.new(0)){|h,x| h[x] += 1; h}.sort_by{|x,n| n}.reverse.map{|(x,n)| [x]*n}.flatten
end```

Code ruby:
```def sort_by_occurrences(xs)
counts = Hash.new(0) # a hash table that maps elements of xs to the number of times the element appears in xs
for x in xs
counts[x] += 1
end
counts. # counts now is {"a"=>1, "b"=>3, "c"=>1, "d"=>2, "e"=>1}
sort_by{|x, count| count}. # sort the elements by the number of times they appear in increasing order: [["a", 1], ["e", 1], ["c", 1], ["d", 2], ["b", 3]]
reverse. # reverse the array because we want elements that appear most first (in decreasing order)
map{|(x,count)| [x]*count}. # replicate each element count times: [["b", "b", "b"], ["d", "d"], ["c"], ["e"], ["a"]]
flatten # put them all in one array
end```

5. maybe simpler
Code:
```ary = [ "a", "b", "b", "b", "c", "d", "d", "e" ]
count = Hash.new(0)
ary.each { |e| count[e] += 1 }
puts ary.sort { |x, y| count[y] - count[x] }```

6. Thanks guys. I started to do something similar a few minutes ago then checked this thread. Good to see I was at least on the right track :-)

7. Stereofrog, that's a much more elegant solution!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•