2

I want to sort this array by two different conditionals.

First I want to sort the array by type: A type can be either (1,2,3,4) and I want to sort them in this order 4 - 1 - 2 - 3.

Then within each different type I want to sort them by a percentage descending.

So a sorted array would look like this:

[
  <OpenStruct percent=70, type=4>,
  <OpenStruct percent=60, type=4>,
  <OpenStruct percent=50, type=4>,
  <OpenStruct percent=73, type=1>,
  <OpenStruct percent=64, type=1>,
  <OpenStruct percent=74, type=2>
]ect

How can I accomplish this sort? Currently I can only sort by type descending.

array = array.sort_by {|r| r.type }
1
  • Why you wanna 4 to go first? Does it make sense to rename types and change 4 to 1, if 4 should always go first? Commented Apr 24, 2015 at 19:49

1 Answer 1

3

This should do it:

require 'ostruct'
arr = [
  OpenStruct.new(percent: 73, type: 1),
  OpenStruct.new(percent: 70, type: 4),
  OpenStruct.new(percent: 60, type: 4),
  OpenStruct.new(percent: 50, type: 4),
  OpenStruct.new(percent: 64, type: 1),
  OpenStruct.new(percent: 74, type: 2)
]


puts arr.sort_by { |a| [a.type % 4, -a.percent] }

output:

#<OpenStruct percent=70, type=4>
#<OpenStruct percent=60, type=4>
#<OpenStruct percent=50, type=4>
#<OpenStruct percent=73, type=1>
#<OpenStruct percent=64, type=1>
#<OpenStruct percent=74, type=2>
Sign up to request clarification or add additional context in comments.

1 Comment

Don't use sort for this. It will be much slower than an implementation using sort_by. See stackoverflow.com/a/2651028/128421 and compare the equivalent sort and sort_by tests.

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.