# Sorting an array basedon two attributes of objects

**URL:** <https://www.ruby-forum.com/t/sorting-an-array-basedon-two-attributes-of-objects/90157>\
**Category:** Ruby\
**Created:** [March 26, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/sorting-an-array-basedon-two-attributes-of-objects/90157 "2007-03-26T14:48:15Z")\
**Posts on this page:** 4\
**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:48pm UTC](https://www.ruby-forum.com/t/sorting-an-array-basedon-two-attributes-of-objects/90157/1 "2007-03-26T14:48:15Z")

</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, 2:54pm UTC](https://www.ruby-forum.com/t/sorting-an-array-basedon-two-attributes-of-objects/90157/2 "2007-03-26T14:54:56Z")

</div>

On Mon, 26 Mar 2007 21:48:17 +0900, senthil  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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.

Is this what you had in mind?

sorted\_employees = employees.sort\_by { |e| [e.salary, e.name] }

-mental

---

<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:55pm UTC](https://www.ruby-forum.com/t/sorting-an-array-basedon-two-attributes-of-objects/90157/3 "2007-03-26T14:55:56Z")

</div>

I think you could try this:

results = person.sort\_by { |p| [p.salary, p.name] }

That should sort by salary first, then name.

-Augie

---

<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:57pm UTC](https://www.ruby-forum.com/t/sorting-an-array-basedon-two-attributes-of-objects/90157/4 "2007-03-26T14:57:56Z")

</div>

On 26 Mrz., 14:48, senthil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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.

Try this: employees.sort\_by { |employee| [employee.salary,  
employee.name] }

Kind regards,  
Florian G.
