# Sort\_by issue on multi criterias

**URL:** <https://www.ruby-forum.com/t/sort-by-issue-on-multi-criterias/148424>\
**Category:** Rails\
**Created:** [September 12, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/sort-by-issue-on-multi-criterias/148424 "2008-09-12T18:18:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [September 12, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/sort-by-issue-on-multi-criterias/148424/1 "2008-09-12T18:18:55Z")

</div>

HI mates

I am trying to sort an array on 2 criterias , one is just in memory  
accessor, other is a record attribute :

I have the following User model :

class User \< ActiveRecord::Base

# name is an attribute in the table

…  
attr\_accessor :current\_distance  
…  
end

In my controller, I calculate current\_distance for each customer, and  
set user.current\_distance , but don’t save the record…

then get the following array :  
all\_users =\>  
[#\<User id: 1, name: “Alan”, created\_at: “2008-09-11 14:30:38”\>,  
#\<User id: 2, name: "Bill, created\_at: “2008-09-11 14:30:38”\>,  
#\<User id: 3, name: “John”, created\_at: “2008-09-11 14:30:38”\>,  
#\<User id: 4, name: “Michael”, created\_at: “2008-09-11 14:30:38”\>]

that I can sort easily using  
all\_users.sort\_by {|item| item.send ‘current\_distance’ }

obviously, I cannot use all\_users.sort\_by {|user|  
user[:current\_distance] } … but I can use all\_users.sort\_by {|  
user| user[:name] }

my issue is trying to sort\_by both criteria 1st on  
current\_distance ASC, and 2nd on name DESC

any enlightment ?

thanks for your help

erwin

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [September 12, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/sort-by-issue-on-multi-criterias/148424/2 "2008-09-12T18:30:03Z")

</div>

just found a piece of the problem :

all\_users.sort\_by{|user| [user.send(‘current\_distance’), user.name]}

seems to work , but how can I set the sort order ? DESC on  
current\_distance et ASC on name ???

thanks
