# "reversing" a Time

**URL:** <https://www.ruby-forum.com/t/reversing-a-time/117184>\
**Category:** Ruby\
**Created:** [October 14, 2007, 2:28pm UTC](https://www.ruby-forum.com/t/reversing-a-time/117184 "2007-10-14T14:28:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mortee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mortee](https://www.ruby-forum.com/u/mortee)\
**Post date:** [October 14, 2007, 2:28pm UTC](https://www.ruby-forum.com/t/reversing-a-time/117184/1 "2007-10-14T14:28:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mortee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mortee](https://www.ruby-forum.com/u/mortee)\
**Post date:** [October 14, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/reversing-a-time/117184/2 "2007-10-14T14:45:40Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![mortee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mortee](https://www.ruby-forum.com/u/mortee)\
**Post date:** [October 14, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/reversing-a-time/117184/3 "2007-10-14T15:21:00Z")

</div>

Couldn’t you just do

sort\_by{|e| [e.name, e.time]}.reverse

?

---

<div class="post-metadata">

**Author:** ![mortee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mortee](https://www.ruby-forum.com/u/mortee)\
**Post date:** [October 15, 2007, 9:34am UTC](https://www.ruby-forum.com/t/reversing-a-time/117184/4 "2007-10-15T09:34:13Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![mortee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mortee](https://www.ruby-forum.com/u/mortee)\
**Post date:** [October 15, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/reversing-a-time/117184/5 "2007-10-15T15:59:33Z")

</div>

SpringFlowers AutumnMoon wrote:

> if the time is just the UNIX time can’t you negate it too? can use  
> to\_f first.

Daniel S. wrote:

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

Yeah, this is what I was looking for.

> how about just use sort {|x, y| [y.name, x.time] \<=\> [x.name, y.time] }  
> so now the name is “negated”.

… and this is right what I wanted to avoid.

mortee

---

<div class="post-metadata">

**Author:** ![mortee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mortee](https://www.ruby-forum.com/u/mortee)\
**Post date:** [October 15, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/reversing-a-time/117184/6 "2007-10-15T15:55:26Z")

</div>

Sascha A. wrote:

> Couldn’t you just do
> 
> sort\_by{|e| [e.name, e.time]}.reverse
> 
> ?

No, because I just wanted to reverse the order of one component, not the  
entire sequence.

mortee

---

<div class="post-metadata">

**Author:** ![mortee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mortee](https://www.ruby-forum.com/u/mortee)\
**Post date:** [October 17, 2007, 6:10am UTC](https://www.ruby-forum.com/t/reversing-a-time/117184/7 "2007-10-17T06:10:38Z")

</div>

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

> 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
