[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"reversing" a Time

mortee

10/14/2007 12:28:00 PM

7 Answers

SpringFlowers AutumnMoon

10/14/2007 12:46:00 PM

0

mortee wrote:
> Hello,
>
> is it possible to reverse order when using a Time as sort key for
> sort_by?
>
> I know I could use sort, and time2 <=> time1, but it's getting
> complicated when the sort key is compound, like
>
> sort_by{|e| [e.name, e.time]}
>
> With e.g. numerics I can simply negate the value for reverse order, but
> I can't negate a Time (or a String, for that matter).

if the time is just the UNIX time can't you negate it too? can use
to_f first.

and then to negate a string probably is too expensive... using 255 to
subtract its ASCII value or XOR each character with 0xFF.

how about just use sort {|x, y| [y.name, x.time] <=> [x.name, y.time] }
so now the name is "negated".
--
Posted via http://www.ruby-....

Sascha Abel

10/14/2007 1:21:00 PM

0

Couldn't you just do

sort_by{|e| [e.name, e.time]}.reverse

?
--
Posted via http://www.ruby-....

Daniel Sheppard

10/15/2007 7:34:00 AM

0

> is it possible to reverse order when using a Time as sort key
> for sort_by?
>
> I know I could use sort, and time2 <=> time1, but it's getting
> complicated when the sort key is compound, like
>
> sort_by{|e| [e.name, e.time]}

For your special case of using time as a sort key, just convert the time
to an integer

sort_by {|e| [e.name, -e.time.to_i] }

Dan.

mortee

10/15/2007 1:54:00 PM

0

mortee

10/15/2007 1:58:00 PM

0

Ken Bloom

10/17/2007 4:08:00 AM

0

On Sun, 14 Oct 2007 21:27:53 +0900, mortee wrote:

> Hello,
>
> is it possible to reverse order when using a Time as sort key for
> sort_by?
>
> I know I could use sort, and time2 <=> time1, but it's getting
> complicated when the sort key is compound, like
>
> sort_by{|e| [e.name, e.time]}
>
> With e.g. numerics I can simply negate the value for reverse order, but
> I can't negate a Time (or a String, for that matter).
>
> thx
> mortee

The integer solutions others have given are more elegant, but just for
completeness...

I believe sort_by implements a stable sort, meaning it doesn't change the
order of equivalent elements, so you could sort

sort_by{|e| e.time}.reverse.sort_by{|e| e.name}

--Ken

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Ian J. Ball

7/26/2013 3:16:00 PM

0

In article <qqa3v85piiuntrab38c21lbvt9uc0rkel7@4ax.com>,
David <dimlan17@yahoo.com> wrote:

> On Thu, 25 Jul 2013 09:29:15 -0700, "Ian J. Ball"
> <ijball-NO_SPAM@mac.invalid> wrote:
>
> >In article <50c39bdf-ad12-46ab-8433-ab7fed14dd36@googlegroups.com>,
> > tmc1982@gmail.com wrote:
> >
> >> https://www.facebook.com/BoycottN...
> >>
> >> Community
> >> Why are pedophiles allowed to be on Nickelodeon staff? Anyone who still
> >> allows their children to watch Nickelodeon after doing a simple Google
> >> search for the name Dan Schneider - WANTS their child to get molested.
> >
> >I'd rather boycott them until Schneider improves "Sam & Cat".
> >
> >Or, better yet - boycott them until NICK agrees to do a 2-hour 'wrap-up'
> >movie for "Victorious"!...
>
> Does it need a 2-hour movie? Why not a 12-hour miniseries??

I'm just trying to be realistic here...

--
"Surf-crazed aliens... Of course." - Amber, "Alien Surf Girls",
Episode #1.1, "Wipeout".
Wait a minute... "Of course"?! "*Of course*"?!! Did I miss a step here??!!