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.

6
  • Can you tell us, what "this code doesn't work" means?
    – Teemu
    Commented Mar 19, 2018 at 18:32
  • Why not use the built-in JavaScript .sort() method on the array? It's more flexible than the lodash sort.
    – Pointy
    Commented Mar 19, 2018 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. Commented Mar 19, 2018 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 Commented Mar 19, 2018 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. Commented Mar 19, 2018 at 18:51

3 Answers 3

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);

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

0
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.