1

Help please me with sorting. I have to sort object with custom string date

var object = [{
  name: "something",
  date: "23.12.2016"
},
{
  name: "something2",
  date: "19.12.2016"
}]

how sort object like this? I already tried with sort function return new Date(a.date) - new Date(b.date); but had nothing it doesn't work Thanks !

2
  • 1
    is the date padded with zero, eg 01.01.2017? Commented Jan 6, 2017 at 11:51
  • 1
    yes it starts from 0 Commented Jan 6, 2017 at 11:53

3 Answers 3

2

You could split the date and rearrange it as iso date and sort by string with localeCompare.

var object = [{ name: "something", date: "23.12.2016" }, { name: "something2", date: "19.12.2016" }];

object.sort(function (a, b) {
    var aa = a.date.split('.'),
        bb = b.date.split('.');
  
    return [aa[2], aa[1], aa[0]].join('-').localeCompare([bb[2], bb[1], bb[0]].join('-'));
});

console.log(object);

You could use as well String#replace for reordering the string.

var object = [{ name: "something", date: "23.12.2016" }, { name: "something2", date: "19.12.2016" }];

object.sort(function (a, b) {
    var aa = a.date.replace(/(..).(..).(....)/, '$3-$2-$1'),
        bb = b.date.replace(/(..).(..).(....)/, '$3-$2-$1');

    return aa.localeCompare(bb);
});

console.log(object);

Sign up to request clarification or add additional context in comments.

Comments

1

The solution using Date.parse() and String.prototype.replace() functions(to sort by timestamps):

var date_objects = [{ name: "something", date: "23.12.2016" }, { name: "something2", date: "19.12.2016" }];

date_objects.sort(function(a,b){
    return Date.parse(a.date.replace(/^(\d{2})\.(\d{2})\.(\d{4})$/, '$3-$2-$1'))
            - Date.parse(b.date.replace(/^(\d{2})\.(\d{2})\.(\d{4})$/, '$3-$2-$1'))
});

console.log(date_objects);

Comments

1

Out of curiosity, I tried a variation about Nina Scholz's answer, maybe it's faster, maybe not. I find reformatting the date quite obvious, but it's also time-consuming. In my version the duplicate indices are probably a problem, but as you stated that the format is zero-padded, this will work:

var object = [{ name: "something", date: "23.12.2016" }, { name: "something2", date: "19.12.2016" }];

object.sort(function (a, b) {
    return (a.date[6] - b.date[6]) 
        || (a.date[7] - b.date[7])
        || (a.date[8] - b.date[8])
        || (a.date[9] - b.date[9])
        || (a.date[3] - b.date[3])
        || (a.date[4] - b.date[4])
        || (a.date[0] - b.date[0])
        || (a.date[1] - b.date[1]);
});

console.log(object);

3 Comments

nice idea, but you could use year, month and day together with slice.
@NinaScholz Thanks for the hint :) Is slicing cheap? Is the "bracketed" access cheap at all? (I'm used to it from C/C++)
@NinaScholz concerning using single chars (although it seems not to be portable): I spare parsing of multi-digit decimals integers from strings because all is of fixed size and all chars are. But well Javascript does a lot of parsing...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.