1

Basically I have an array of dates:

var array = ["2017-01-22 00:21:17.0",
           "2017-01-27 11:30:23.0",
           "2017-01-24 15:53:21.0",
           "2017-01-27 11:34:18.0",
           "2017-01-26 16:55:48.0",
           "2017-01-22 11:57:12.0",
           "2017-01-27 11:35:43.0"];

Which I need to sort this based on max and min date; I tried:

var lowest = _.max(array, function(o){return o.val;});
console.log(lowest);

But this returns -Infinity and _.min returns Infinity

Regards :)

Jay wardan
  • 131
  • 11
  • Possible duplicate of [Sort Javascript Object Array By Date](http://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date) – Damon Jan 27 '17 at 14:57
  • Simply get rid of the `function` part: `_.min(array), _.max(array)` – georg Jan 27 '17 at 19:05

2 Answers2

0

You could use Array#sort without callback. This sorts by string, which gets the min value at index zero and at the last index the max date.

var array = ["2017-01-22 00:21:17.0", "2017-01-27 11:30:23.0", "2017-01-24 15:53:21.0", "2017-01-27 11:34:18.0", "2017-01-26 16:55:48.0", "2017-01-22 11:57:12.0", "2017-01-27 11:35:43.0"];

array.sort();
console.log(array);
console.log('min', array[0]);
console.log('max', array[array.length - 1]);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Another solution could be the use of Array#reduce.

var array = ["2017-01-22 00:21:17.0", "2017-01-27 11:30:23.0", "2017-01-24 15:53:21.0", "2017-01-27 11:34:18.0", "2017-01-26 16:55:48.0", "2017-01-22 11:57:12.0", "2017-01-27 11:35:43.0"],
    result = array.reduce(function (r, a, i) {
        if (!i) {
            return { min: a, max: a};
        }
        if (a < r.min) {
            r.min = a;
        }
        if (a > r.max) {
            r.max = a;
        }
        return r;
    }, undefined);

console.log(result);
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
  • So I suppose in order to get the max value, I need to read array in reverse order? Okay I didn't thought it might be this easy. – Jay wardan Jan 27 '17 at 14:49
  • be very careful using the first method. The default sort order is according to string Unicode points and has nothing to do with comparing dates. There are probably edge cases where it falls apart (especially if you have any formatted differently), but it will work on the array you provided. – Damon Jan 27 '17 at 14:55
  • @damon Thanks, That is the only sort of array I'm gonna have for sorting. – Jay wardan Jan 27 '17 at 15:03
  • @Damon, ISO date strings are sortable as strings, there are no edge cases. – Nina Scholz Jan 27 '17 at 18:51
  • @NinaScholz good to know, I figured if the formatting is valid it will work properly. – Damon Jan 27 '17 at 18:57
0

var array = ["2017-01-22 00:21:17.0",
           "2017-01-27 11:30:23.0",
           "2017-01-24 15:53:21.0",
           "2017-01-27 11:34:18.0",
           "2017-01-26 16:55:48.0",
           "2017-01-22 11:57:12.0",
           "2017-01-27 11:35:43.0"];

var timeStamped = array.map((item) => new Date(item).getTime());

console.log('Max is ', new Date(Math.max.apply(null,timeStamped)). toUTCString());

console.log('Min is ', new Date(Math.min.apply(null,timeStamped)). toUTCString());
Tareq
  • 4,537
  • 2
  • 11
  • 17