# Sorting an array based on two attributes of objects

**URL:** <https://www.ruby-forum.com/t/sorting-an-array-based-on-two-attributes-of-objects/102725>\
**Category:** Rails\
**Created:** [March 26, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-two-attributes-of-objects/102725 "2007-03-26T14:50:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![senthil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@senthil](https://www.ruby-forum.com/u/senthil)\
**Post date:** [March 26, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-two-attributes-of-objects/102725/1 "2007-03-26T14:50:02Z")

</div>

Hi all,  
I want to sort the objects of array based on two attributes.I want  
sort an employee class based on his salary and name , so that if two  
person has same salary it should be sorted with name.  
Lets say for example the employee objects has following name and  
salary.

```
     name salary
       d 100
       c 200
       b 50
       a 100

 so in this case the result which i expect is

     name salary
       c 50
       a 100
       d 100
       b 200

```

Note:In the above example for salary 100 the sorting is done  
alphabetically, but initially(before sorting) ‘d’ came first and then  
‘a’ came.so basically i want to sort the array with more than one order.  
Can any one help me to solve it ??

---

<div class="post-metadata">

**Author:** ![senthil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@senthil](https://www.ruby-forum.com/u/senthil)\
**Post date:** [March 26, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-two-attributes-of-objects/102725/2 "2007-03-26T15:26:02Z")

</div>

senthil wrote:

> ```
> b 50
> 
> ```
> 
> Note:In the above example for salary 100 the sorting is done  
> alphabetically, but initially(before sorting) ‘d’ came first and then  
> ‘a’ came.so basically i want to sort the array with more than one order.  
> Can any one help me to solve it ??

Use sort\_by and have it return an array of the two objects you want  
sorted:

[26]irb  
irb(main):001:0\> myarray = [{:name =\> “d”, :salary =\> 100}, {:name =\>  
“c”, :salary =\> 200}, {:name =\> “a”, :salary =\> 100}]  
=\> [{:salary=\>100, :name=\>“d”}, {:salary=\>200, :name=\>“c”},  
{:salary=\>100, :name=\>“a”}]  
irb(main):002:0\> myarray.sort\_by {|h| [h[:salary], h[:name]] }  
=\> [{:salary=\>100, :name=\>“a”}, {:salary=\>100, :name=\>“d”},  
{:salary=\>200, :name=\>“c”}]

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![senthil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@senthil](https://www.ruby-forum.com/u/senthil)\
**Post date:** [March 26, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-two-attributes-of-objects/102725/3 "2007-03-26T16:00:11Z")

</div>

On Mar 26, 2007, at 8:50 AM, senthil wrote:

> ```
> c 200
> b 200
> 
> ```
> 
> Note:In the above example for salary 100 the sorting is done  
> alphabetically, but initially(before sorting) ‘d’ came first and then  
> ‘a’ came.so basically i want to sort the array with more than one  
> order.  
> Can any one help me to solve it ??

[[‘d’,100], [‘c’,200], [‘b’,50], [‘a’,100] ].sort\_by {|ns|  
[ns.last, ns.first]}  
=\> [[“b”, 50], [“a”, 100], [“d”, 100], [“c”, 200]]

An array compares on its elements.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
