0

I have following array which contains strings;

let data = ["2018-1", "2018-5", "2018-11", "2018-2", "2018-10", "2018-12"];

these strings are composed of number (year and month).

Can you tell me why didn't work following function for sorting? I need sort this array from latest date to oldest. In this case from "2018-12" to "2018-1".

I am using lodash in whole project so I try use it here as well.

var result = _.sortBy(data, function(i) {
  var x = i.split("-").map(Number);
  return [x[0], x[1]];
});

can you tell me why this code doesn't work and how to fix it? Thanks.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Can you tell us, what "_this code doesn't work_" means? – Teemu Mar 19 '18 at 18:32
  • Why not use the built-in JavaScript `.sort()` method on the array? It's more flexible than the lodash sort. – Pointy Mar 19 '18 at 18:34
  • 1
    the missing of leading zero for month smaller than 10 makes sorting complicated. the most easy solution would using a leading zero for month numbers and use the date string directly. – Nina Scholz Mar 19 '18 at 18:38
  • The first error that I got is `Uncaught ReferenceError: _ is not defined` What do you mean with "this code doesn't work", show me the error, please – Yago Azedias Mar 19 '18 at 18:39
  • the problem with this question is, you have the date in a niche sortable format, but you change it to a unsortable format for the final result. the question now is a philosophical approach to change the original given well formated back to a wrong, but needed, or just to use the well formatted date and later change the date to the wanted format. – Nina Scholz Mar 19 '18 at 18:51
  • Next time, please don't tag "lodash" if you're looking for vanilla code. – georg Mar 19 '18 at 19:18

3 Answers3

2

I added a few more dates as proof.

let data = ["2018-1", "2018-5", "2018-11", "2018-2", "2018-10", "2018-12", "2017-5", "2019-12"];

var result = data.sort((a, b) => {
  var n1 = a.split("-");
  var n2 = b.split("-");
  n1 = parseInt(n1[0]) * 100 + parseInt(n1[1]);
  n2 = parseInt(n2[0]) * 100 + parseInt(n2[1]);
  return n1 - n2;
})

console.log(result);
georg
  • 211,518
  • 52
  • 313
  • 390
Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38
1

Unfortunately, sortBy doesn't support compound keys, so your array key is converted to a string. A workaround is either to provide two separate keys:

var result = _.sortBy(data, [
    x => Number(x.split('-')[0]),
    x => Number(x.split('-')[1]),
]);

or synthesize a numeric one:

var result = _.sortBy(data, x => {
    x = x.split('-');
    return Number(x[0]) * 1000 + Number(x[1])
});

Finally, you can take a risk and try Date.parse:

var result = _.sortBy(data, Date.parse)

which looks neat, but requires some cross-browser testing.

georg
  • 211,518
  • 52
  • 313
  • 390
0

You could chain the splitted deltas for year and month.

var data = ["2018-1", "2018-5", "2018-11", "2018-2", "2018-10", "2018-12", "2017-5", "2019-12"],
    result = data.sort((a, b) => {
        var aa = a.split("-"),
            bb = b.split("-");

        return aa[0] - bb[0] || aa[1] - bb[1];
    });

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392