sortung a structured array on more than one criteria

J

Josselin

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

and I got obviously an error ... parse error, unexpected '}',
expecting tCOLON2 or '[' or '.'

canot find the correct writing..

appreciate any help...

thanks

joss
 
J

James Edward Gray II

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

results = rs.sort_by { |p| [p.km, p.updated_at] }

Arrays can be sorted and will compare their contents in order, so
this should do what you want.

Hope that helps.

James Edward Gray II
 
D

dblack

Hi --

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

and I got obviously an error ... parse error, unexpected '}', expecting
tCOLON2 or '[' or '.'

canot find the correct writing..

Try this:

results = rs.sort_by {|p| [p.km, p.updated_at] }


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
J

Josselin

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

and I got obviously an error ... parse error, unexpected '}',
expecting tCOLON2 or '[' or '.'

canot find the correct writing..

appreciate any help...

thanks

joss

thanks to both of you... and HNY%2007 (Dry version of Happy New Year
2007) ;-))

I forgot to ask about a DESC sort.... p.km is Ascending and
p.updated_at should be DESC..

I am not sure that is possible (at least not documented.....)

joss
 
F

Farrel Lifson

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

and I got obviously an error ... parse error, unexpected '}',
expecting tCOLON2 or '[' or '.'

canot find the correct writing..

appreciate any help...

thanks

joss

results = rs.sort_by{|p| [p.km,p.updated_at]}

Farrel
 
J

Jan Svitok

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

and I got obviously an error ... parse error, unexpected '}',
expecting tCOLON2 or '[' or '.'

canot find the correct writing..

appreciate any help...

thanks

joss

thanks to both of you... and HNY%2007 (Dry version of Happy New Year
2007) ;-))

I forgot to ask about a DESC sort.... p.km is Ascending and
p.updated_at should be DESC..

I am not sure that is possible (at least not documented.....)

joss

results = rs.sort_by{|p| p.km, -p.updated_at } # if updated_at is Numeric

or

results = rs.sort { |a,b| [a.km, b.updated_at] <=> [b.km, a.updated_at] }
 
J

Josselin

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

and I got obviously an error ... parse error, unexpected '}',
expecting tCOLON2 or '[' or '.'

canot find the correct writing..

appreciate any help...

thanks

joss

thanks to both of you... and HNY%2007 (Dry version of Happy New Year
2007) ;-))

I forgot to ask about a DESC sort.... p.km is Ascending and
p.updated_at should be DESC..

I am not sure that is possible (at least not documented.....)

joss

results = rs.sort_by{|p| p.km, -p.updated_at } # if updated_at is Numeric

or

results = rs.sort { |a,b| [a.km, b.updated_at] <=> [b.km, a.updated_at] }

thanks , 'updated_at' is a string ( "2007-01-04 13:48:30")
I try to understand the writing... why the first parameter will be
Ascending and the second Descending ...

how would you write it, if both parameters shoudl be Descending ?

Joss
 
D

dblack

Hi --

thanks to both of you... and HNY%2007 (Dry version of Happy New Year 2007)
;-))

Actually Happy New Year 2007 is already pretty DRY, although it does
use two 0's.... :) Yours is more like the golf version :)


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
D

dblack

Hi --
On 2007-01-09 15:29:43 +0100, Josselin <[email protected]> said:

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

and I got obviously an error ... parse error, unexpected '}',
expecting tCOLON2 or '[' or '.'

canot find the correct writing..

appreciate any help...

thanks

joss

thanks to both of you... and HNY%2007 (Dry version of Happy New Year
2007) ;-))

I forgot to ask about a DESC sort.... p.km is Ascending and
p.updated_at should be DESC..

I am not sure that is possible (at least not documented.....)

joss

results = rs.sort_by{|p| p.km, -p.updated_at } # if updated_at is Numeric

or

results = rs.sort { |a,b| [a.km, b.updated_at] <=> [b.km, a.updated_at] }

thanks , 'updated_at' is a string ( "2007-01-04 13:48:30")
I try to understand the writing... why the first parameter will be Ascending
and the second Descending ...

how would you write it, if both parameters shoudl be Descending ?

results = rs.sort_by {...}.reverse


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
J

Jan Svitok

Hi --
On 2007-01-09 15:29:43 +0100, Josselin <[email protected]> said:

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

and I got obviously an error ... parse error, unexpected '}',
expecting tCOLON2 or '[' or '.'

canot find the correct writing..

appreciate any help...

thanks

joss

thanks to both of you... and HNY%2007 (Dry version of Happy New Year
2007) ;-))

I forgot to ask about a DESC sort.... p.km is Ascending and
p.updated_at should be DESC..

I am not sure that is possible (at least not documented.....)

joss

results = rs.sort_by{|p| p.km, -p.updated_at } # if updated_at is Numeric

or

results = rs.sort { |a,b| [a.km, b.updated_at] <=> [b.km, a.updated_at] }

thanks , 'updated_at' is a string ( "2007-01-04 13:48:30")
I try to understand the writing... why the first parameter will be Ascending
and the second Descending ...

how would you write it, if both parameters shoudl be Descending ?

results = rs.sort_by {...}.reverse

Ok, two things:

1. how does my code works:

The block in sort {|x,y| } implements x<=>y (<=>, comparison operator,
sometimes called starship operator, returns -1, 0 or 1 for x <y, x==y
and x> y)

[a,b] <=> [c,d] (array comparision) is implemented as first doing
a<=>c and if they are equal doing b<=>d. if I exchange b and d, I
effectively reverse the result of the second comparision.

If I wanted the reverse sort, I could exchange both pairs, i.e.
instead of doing x<=>y I'd be doing y <=> x.

2. sort_by is usually faster than sort because it does n block
evaluations, while sort does O(n*n) evaluations. Therefore sort_by
{}.reverse is usually better than sort{|a,b| b<=> a} (except some edge
cases where computaton is fast, and the block's result is large)
 
M

Mark Volkmann

I can sort an array like that :

results = rs.sort_by{|p| p.km}

but I tried to add a scrond sorting criteria like that :

results = rs.sort_by{|p| p.km, p.updated_at }

Try this.

results = rs.sort_by { |p| [p.km, p.updated_at] }

This way you're sorting arrays containing each of the sort criteria.
and I got obviously an error ... parse error, unexpected '}',
expecting tCOLON2 or '[' or '.'

canot find the correct writing..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,137
Members
47,754
Latest member
Armand37T7

Latest Threads

Top