Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #25855 > unrolled thread

Ordering time stamp

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2014-09-15 14:17 +1000
Last post2014-09-16 14:46 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Ordering time stamp Andrew Poulos <ap_prog@hotmail.com> - 2014-09-15 14:17 +1000
    Re: Ordering time stamp Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-09-15 13:55 +0100
    Re: Ordering time stamp Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-16 02:30 +0000
      Re: Ordering time stamp Andrew Poulos <ap_prog@hotmail.com> - 2014-09-16 13:29 +1000
        Re: Ordering time stamp Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-16 14:46 +0000

#25855 — Ordering time stamp

FromAndrew Poulos <ap_prog@hotmail.com>
Date2014-09-15 14:17 +1000
SubjectOrdering time stamp
Message-ID<PMKdnU_uWMGj9IvJnZ2dnUVZ_u2dnZ2d@westnet.com.au>
A piece of elearning I'm working generates JSON as an array of objects 
(from a "new" standard called Tin Can). Each object has a timestamp 
property.

How do I order the array from the time stamp where a time stamp looks 
like one of these:
   timestamp: "2014-09-15T03:27:16.017Z"
   timestamp: "2014-09-15T03:27:17.023Z"
   timestamp: "2014-09-15T03:27:17.029Z"

The standard's example js shows the generation of a time stamp with this

if (this.timestamp === null) {
   this.timestamp = TinCan.Utils.getISODateString(new Date());
}

Can I simply do something based on (for example):

var x = new Date("2014-09-15T03:27:16.017Z");
var y = new Date("2014-09-15T03:27:17.023Z");

if (x > y) {
   // x is newer
} else {
   // y is newer
}

Andrew Poulos

[toc] | [next] | [standalone]


#25856

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-09-15 13:55 +0100
Message-ID<87egvd3ux2.fsf@bsb.me.uk>
In reply to#25855
Andrew Poulos <ap_prog@hotmail.com> writes:

> A piece of elearning I'm working generates JSON as an array of objects
> (from a "new" standard called Tin Can). Each object has a timestamp
> property.
>
> How do I order the array from the time stamp where a time stamp looks
> like one of these:
>   timestamp: "2014-09-15T03:27:16.017Z"
>   timestamp: "2014-09-15T03:27:17.023Z"
>   timestamp: "2014-09-15T03:27:17.029Z"
>
> The standard's example js shows the generation of a time stamp with this
>
> if (this.timestamp === null) {
>   this.timestamp = TinCan.Utils.getISODateString(new Date());
> }
>
> Can I simply do something based on (for example):
>
> var x = new Date("2014-09-15T03:27:16.017Z");
> var y = new Date("2014-09-15T03:27:17.023Z");
>
> if (x > y) {
>   // x is newer
> } else {
>   // y is newer
> }

Yes, but that hides the conversion that's being done.  It may be better
to explicitly compare the times so you don't get a surprise when using
==.

  if (x.getTime() > y.getTime())

However, if the timestamps are always in the form shown (and you don't
need the Date objects for some other reason), you could just compare the
strings without making Date objects from them.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#25857

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-09-16 02:30 +0000
Message-ID<lv87bi$anv$1@dont-email.me>
In reply to#25855
On Mon, 15 Sep 2014 14:17:32 +1000, Andrew Poulos wrote:

> A piece of elearning I'm working generates JSON as an array of objects
> (from a "new" standard called Tin Can). Each object has a timestamp
> property.
> 
> How do I order the array from the time stamp where a time stamp looks
> like one of these:
>    timestamp: "2014-09-15T03:27:16.017Z"
>    timestamp: "2014-09-15T03:27:17.023Z"
>    timestamp: "2014-09-15T03:27:17.029Z"
> 
> The standard's example js shows the generation of a time stamp with this
> 
> if (this.timestamp === null) {
>    this.timestamp = TinCan.Utils.getISODateString(new Date());
> }
> 
> Can I simply do something based on (for example):
> 
> var x = new Date("2014-09-15T03:27:16.017Z");
> var y = new Date("2014-09-15T03:27:17.023Z");
> 
> if (x > y) {
>    // x is newer
> } else {
>    // y is newer
> }

func cmpab(a,b) {
if ( a.timestamp > b.timestamp ) return 1;
if ( a.timestamp > b.timestamp ) return -1;
return 0;
}

Then given an array of objects that have a timestamp string prop you can 
sort the array using:

arr.sort(cmpab);

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#25858

FromAndrew Poulos <ap_prog@hotmail.com>
Date2014-09-16 13:29 +1000
Message-ID<hNKdneD09sQTMorJnZ2dnUVZ_uednZ2d@westnet.com.au>
In reply to#25857
On 16/09/2014 12:30 PM, Denis McMahon wrote:
> On Mon, 15 Sep 2014 14:17:32 +1000, Andrew Poulos wrote:
>
>> A piece of elearning I'm working generates JSON as an array of objects
>> (from a "new" standard called Tin Can). Each object has a timestamp
>> property.
>>
>> How do I order the array from the time stamp where a time stamp looks
>> like one of these:
>>     timestamp: "2014-09-15T03:27:16.017Z"
>>     timestamp: "2014-09-15T03:27:17.023Z"
>>     timestamp: "2014-09-15T03:27:17.029Z"
>>
>> The standard's example js shows the generation of a time stamp with this
>>
>> if (this.timestamp === null) {
>>     this.timestamp = TinCan.Utils.getISODateString(new Date());
>> }
>>
>> Can I simply do something based on (for example):
>>
>> var x = new Date("2014-09-15T03:27:16.017Z");
>> var y = new Date("2014-09-15T03:27:17.023Z");
>>
>> if (x > y) {
>>     // x is newer
>> } else {
>>     // y is newer
>> }
>
> func cmpab(a,b) {
> if ( a.timestamp > b.timestamp ) return 1;
> if ( a.timestamp > b.timestamp ) return -1;
> return 0;
> }

Thanks, but I guess you mean for the second if
   if ( a.timestamp < b.timestamp ) return -1;

> Then given an array of objects that have a timestamp string prop you can
> sort the array using:
>
> arr.sort(cmpab);

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#25859

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-09-16 14:46 +0000
Message-ID<lv9ifm$7fp$2@dont-email.me>
In reply to#25858
On Tue, 16 Sep 2014 13:29:47 +1000, Andrew Poulos wrote:

> Thanks, but I guess you mean for the second if
>    if ( a.timestamp < b.timestamp ) return -1;

Yes, oops. ;)

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web