# How to sort a multi-elements array

**URL:** <https://rubytalk.org/t/how-to-sort-a-multi-elements-array/46833>\
**Category:** ruby-talk\
**Created:** [31 May 2008 17:34 UTC](https://rubytalk.org/t/how-to-sort-a-multi-elements-array/46833 "2008-05-31T17:34:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erwin](https://avatars.discourse-cdn.com/v4/letter/e/a88e57/32.png) [@Erwin](https://rubytalk.org/u/Erwin)\
**Post date:** [31 May 2008 17:34 UTC](https://rubytalk.org/t/how-to-sort-a-multi-elements-array/46833/1 "2008-05-31T17:34:39Z")

</div>

I know how to sort an array with 2 elements... that's the only example  
I could fin on Array#sort...  
but what if ... I need to sort an array like this one :

anArray = [[1, "joe", 4], [2, "arthur", 2], [5, "william", 3], [16,  
"bob", 1], [20, "ernesto", 17], [27, "julia", 1], [28, "barbara", 2] ]

sorting on third-element (highest ranking) , and second-element  
(nickname)

aSortedArray = [[20, "ernesto", 17], [1, "joe", 4], 5, "william", 3],  
[2, "arthur", 2], [28, "barbara", 2], [16, "bob", 1] ]

any doc link where this is explained ?

thanks a lot for your tip

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [31 May 2008 17:42 UTC](https://rubytalk.org/t/how-to-sort-a-multi-elements-array/46833/2 "2008-05-31T17:42:22Z")

</div>

You can do this:

aSortedArray = anArray.sort do |a, b|  
&nbsp;&nbsp;&nbsp;if a[2] == b[2] then a[1] \<=\> b[1]  
&nbsp;&nbsp;&nbsp;else a[2] \<=\> b[2]  
&nbsp;&nbsp;&nbsp;end  
end

If the third element of the two subarrays are equal, then the block returns  
the result of the comparison of the second elements (using \<=\>), otherwise it  
returns the result of the comparison of the third elements, again using \<=\>.

I hope this helps

Stefano

> **···**
>
> On Saturday 31 May 2008, Erwin wrote:
> 
> > I know how to sort an array with 2 elements... that's the only example  
> > I could fin on Array#sort...  
> > but what if ... I need to sort an array like this one :
> > 
> > anArray = [[1, "joe", 4], [2, "arthur", 2], [5, "william", 3], [16,  
> > "bob", 1], [20, "ernesto", 17], [27, "julia", 1], [28, "barbara", 2] ]
> > 
> > sorting on third-element (highest ranking) , and second-element  
> > (nickname)
> > 
> > aSortedArray = [[20, "ernesto", 17], [1, "joe", 4], 5, "william", 3],  
> > [2, "arthur", 2], [28, "barbara", 2], [16, "bob", 1] ]
> > 
> > any doc link where this is explained ?
> > 
> > thanks a lot for your tip

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [31 May 2008 17:44 UTC](https://rubytalk.org/t/how-to-sort-a-multi-elements-array/46833/3 "2008-05-31T17:44:37Z")

</div>

As always, use #sort with a block or #sort\_by - the number of elements in those arrays is irrelevant, you just need to decide which elements you want to pick as sort key.

an\_array.sort\_by {|a,b,c| [c,b]}  
an\_array.sort {|(a,b,c),(d,e,f)| [c,b] \<=\> [f,e]}  
an\_array.sort do |(a,b,c),(d,e,f)|  
&nbsp;&nbsp;&nbsp;x = c \<=\> f  
&nbsp;&nbsp;&nbsp;x == 0 ? b \<=\> e : x  
end

Btw, your data looks like it could benefit from using Struct and also benefit from the automatically implemented \<=\>, i.e.

Person = Struct.new :age, :name, :position

an\_array = [  
&nbsp;&nbsp;&nbsp;Person.new(4, "joe", 1),  
&nbsp;&nbsp;&nbsp;Person.new(2, "arthur", 2),  
&nbsp;&nbsp;&nbsp;Person.new(3, "william", 5),  
]

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 31.05.2008 19:31, Erwin wrote:
> 
> > I know how to sort an array with 2 elements... that's the only example  
> > I could fin on Array#sort...  
> > but what if ... I need to sort an array like this one :
> > 
> > anArray = [[1, "joe", 4], [2, "arthur", 2], [5, "william", 3], [16,  
> > "bob", 1], [20, "ernesto", 17], [27, "julia", 1], [28, "barbara", 2] ]
> > 
> > sorting on third-element (highest ranking) , and second-element  
> > (nickname)
> > 
> > aSortedArray = [[20, "ernesto", 17], [1, "joe", 4], 5, "william", 3],  
> > [2, "arthur", 2], [28, "barbara", 2], [16, "bob", 1] ]
> > 
> > any doc link where this is explained ?

---

<div class="post-metadata">

**Author:** ![Erwin](https://avatars.discourse-cdn.com/v4/letter/e/a88e57/32.png) [@Erwin](https://rubytalk.org/u/Erwin)\
**Post date:** [1 June 2008 06:34 UTC](https://rubytalk.org/t/how-to-sort-a-multi-elements-array/46833/4 "2008-06-01T06:34:33Z")

</div>

thanks Robert , I'll write that in my NoteTaker Ruby book !!

> **···**
>
> On 31 mai, 19:40, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> > On 31.05.2008 19:31, Erwin wrote:
> > 
> > \> I know how to sort an array with 2 elements... that's the only example  
> > \> I could fin on Array#sort...  
> > \> but what if ... I need to sort an array like this one :
> > 
> > \> anArray = [[1, "joe", 4], [2, "arthur", 2], [5, "william", 3], [16,  
> > \> "bob", 1], [20, "ernesto", 17], [27, "julia", 1], [28, "barbara", 2] ]
> > 
> > \> sorting on third-element (highest ranking) , and second-element  
> > \> (nickname)
> > 
> > \> aSortedArray = [[20, "ernesto", 17], [1, "joe", 4], 5, "william", 3],  
> > \> [2, "arthur", 2], [28, "barbara", 2], [16, "bob", 1] ]
> > 
> > \> any doc link where this is explained ?
> > 
> > As always, use #sort with a block or #sort\_by - the number of elements  
> > in those arrays is irrelevant, you just need to decide which elements  
> > you want to pick as sort key.
> > 
> > an\_array.sort\_by {|a,b,c| [c,b]}  
> > an\_array.sort {|(a,b,c),(d,e,f)| [c,b] \<=\> [f,e]}  
> > an\_array.sort do |(a,b,c),(d,e,f)|  
> > &nbsp;&nbsp;&nbsp;x = c \<=\> f  
> > &nbsp;&nbsp;&nbsp;x == 0 ? b \<=\> e : x  
> > end
> > 
> > Btw, your data looks like it could benefit from using Struct and also  
> > benefit from the automatically implemented \<=\>, i.e.
> > 
> > Person = Struct.new :age, :name, :position
> > 
> > an\_array = [  
> > &nbsp;&nbsp;&nbsp;Person.new(4, "joe", 1),  
> > &nbsp;&nbsp;&nbsp;Person.new(2, "arthur", 2),  
> > &nbsp;&nbsp;&nbsp;Person.new(3, "william", 5),  
> > ]
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Alexey\_Zilber](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alexey\_Zilber](https://rubytalk.org/u/Alexey_Zilber)\
**Post date:** [7 April 2009 03:29 UTC](https://rubytalk.org/t/how-to-sort-a-multi-elements-array/46833/5 "2009-04-07T03:29:54Z")

</div>

Hi,

&nbsp;&nbsp;Was going through these older posts and have a similar question. I  
have a nested array that contains a date and a filename. I'm trying to  
sort by file date.

The array is in the form:

SortedFiles = [[200904031109, "filea.txt"], [200904031337,  
"fileb.txt"], [200904031110, "filec.xt"] ]

What I was trying to do was:

SortedFiles.sort { |[[a,b],[x,y]] a \< x | }

That, unfortunately, did not work. I'm using Ruby 1.8.5 btw. Can anyone  
help out?

Thanks,  
Alex

Stefano Crocco wrote:

> **···**
>
> > On Saturday 31 May 2008, Erwin wrote:
> > 
> > > aSortedArray = [[20, "ernesto", 17], [1, "joe", 4], 5, "william", 3],  
> > > [2, "arthur", 2], [28, "barbara", 2], [16, "bob", 1] ]
> > > 
> > > any doc link where this is explained ?
> > > 
> > > thanks a lot for your tip
> > 
> > You can do this:
> > 
> > aSortedArray = anArray.sort do |a, b|  
> > &nbsp;&nbsp;&nbsp;if a[2] == b[2] then a[1] \<=\> b[1]  
> > &nbsp;&nbsp;&nbsp;else a[2] \<=\> b[2]  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > If the third element of the two subarrays are equal, then the block  
> > returns  
> > the result of the comparison of the second elements (using \<=\>),  
> > otherwise it  
> > returns the result of the comparison of the third elements, again using  
> > \<=\>.
> > 
> > I hope this helps
> > 
> > Stefano
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Christopher\_Dicely](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Christopher\_Dicely](https://rubytalk.org/u/Christopher_Dicely)\
**Post date:** [7 April 2009 04:31 UTC](https://rubytalk.org/t/how-to-sort-a-multi-elements-array/46833/6 "2009-04-07T04:31:24Z")

</div>

SortedFiles.sort\_by {|date, fname| date}  
or:  
SortedFiles.sort {|record1,record2| record1[0]\<=\>record2[0]}

> **···**
>
> On 4/6/09, Alexey Zilber \<alexeyzilber@gmail.com\> wrote:
> 
> > Hi,
> > 
> > &nbsp;&nbsp;Was going through these older posts and have a similar question. I  
> > have a nested array that contains a date and a filename. I'm trying to  
> > sort by file date.
> > 
> > The array is in the form:
> > 
> > SortedFiles = [[200904031109, "filea.txt"], [200904031337,  
> > "fileb.txt"], [200904031110, "filec.xt"] ]
> > 
> > What I was trying to do was:
> > 
> > SortedFiles.sort { |[[a,b],[x,y]] a \< x | }
> > 
> > That, unfortunately, did not work. I'm using Ruby 1.8.5 btw. Can anyone  
> > help out?

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [7 April 2009 12:26 UTC](https://rubytalk.org/t/how-to-sort-a-multi-elements-array/46833/7 "2009-04-07T12:26:27Z")

</div>

Since you are sorting on the first element of each array you can just use  
SortedFiles.sort

Array comparison with another array works by using the comparison of the  
first pair of elements which aren't equal, so [1,1] \< [2,0] \< [2,1]

> **···**
>
> On Tue, Apr 7, 2009 at 12:31 AM, Christopher Dicely \<cmdicely@gmail.com\>wrote:
> 
> > On 4/6/09, Alexey Zilber \<alexeyzilber@gmail.com\> wrote:  
> > \> Hi,  
> > \>  
> > \> Was going through these older posts and have a similar question. I  
> > \> have a nested array that contains a date and a filename. I'm trying to  
> > \> sort by file date.  
> > \>  
> > \> The array is in the form:  
> > \>  
> > \> SortedFiles = [[200904031109, "filea.txt"], [200904031337,  
> > \> "fileb.txt"], [200904031110, "filec.xt"] ]  
> > \>  
> > \> What I was trying to do was:  
> > \>  
> > \> SortedFiles.sort { |[[a,b],[x,y]] a \< x | }  
> > \>  
> > \> That, unfortunately, did not work. I'm using Ruby 1.8.5 btw. Can anyone  
> > \> help out?
> > 
> > SortedFiles.sort\_by {|date, fname| date}  
> > or:  
> > SortedFiles.sort {|record1,record2| record1[0]\<=\>record2[0]}
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
