Andrew Poulos
9/16/2014 3:30:00 AM
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