Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how to sort a multi-elements array

Reply
Thread Tools

how to sort a multi-elements array

 
 
Erwin
Guest
Posts: n/a
 
      05-31-2008
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
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      05-31-2008
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)|
x = c <=> f
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, osition

an_array = [
Person.new(4, "joe", 1),
Person.new(2, "arthur", 2),
Person.new(3, "william", 5),
]

Kind regards

robert
 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      05-31-2008
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


You can do this:

aSortedArray = anArray.sort do |a, b|
if a[2] == b[2] then a[1] <=> b[1]
else a[2] <=> b[2]
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


 
Reply With Quote
 
Erwin
Guest
Posts: n/a
 
      06-01-2008
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)|
> x = c <=> f
> 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, osition
>
> an_array = [
> Person.new(4, "joe", 1),
> Person.new(2, "arthur", 2),
> Person.new(3, "william", 5),
> ]
>
> Kind regards
>
> robert


thanks Robert , I'll write that in my NoteTaker Ruby book !!
 
Reply With Quote
 
Alexey Zilber
Guest
Posts: n/a
 
      04-07-2009
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?

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|
> if a[2] == b[2] then a[1] <=> b[1]
> else a[2] <=> b[2]
> 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/.

 
Reply With Quote
 
Christopher Dicely
Guest
Posts: n/a
 
      04-07-2009
On 4/6/09, Alexey Zilber <> 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]}

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      04-07-2009
[Note: parts of this message were removed to make it a legal post.]

On Tue, Apr 7, 2009 at 12:31 AM, Christopher Dicely <>wrote:

> On 4/6/09, Alexey Zilber <> 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]}
>
>

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]


--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/pers...-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Merge Sort in C - array output is same as input after sort routine completes rkk C Programming 9 09-24-2006 08:30 PM
Array sort function sorts on chars not numbers ... help ! how to sort numbers GIMME Javascript 5 07-26-2004 01:28 AM
multi-field array sort using Sort::Fields method Domenico Discepola Perl Misc 6 04-28-2004 04:28 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57