0

I need to sort the following array of objects:

let data = [{
    "data": {
        "date_entered": "2021-02-18",
        "order": "2",
    },
},
{
    "data": {
        "date_entered": "2021-02-18",
        "order": "1",
    }
},
{
    "data": {
        "date_entered": "2021-02-15",
        "order": "",
    },
},
{
    "data": {
        "date_entered": "2021-02-18",
        "order": "",
    },
}
];

It firstly needs to sort by the order value and then sort by the date_entered, this is the code that i have written so far, but it seems to display the data3 data4 first instead of data2, data1, data4, data3

return function (a, b) {
        return (
            parseInt(a.data.order) - parseInt(b.data.order) ||
            new Date(b.data.date_entered).getTime() -
                new Date(a.data.date_entered).getTime()
        );
    };

Any ideas as to what I am doing wrong? would greatly appreciate any help :)

amature
  • 1
  • 1
  • 1
    where is the connection to a `null` value as mentioned with the topic? – Peter Seliger Feb 19 '21 at 17:38
  • where do you want the `''` values to go? – Nina Scholz Feb 19 '21 at 17:38
  • @PeterSeliger He means null string, i.e. empty string. – Barmar Feb 19 '21 at 17:38
  • 1
    I'm aware of that, I just want the OP to precisely formulate the topic for others who want to contribute in solving the OP's task and who can not make the connection. – Peter Seliger Feb 19 '21 at 17:41
  • *"but it seems to display the data3 data4 first instead of data2, data1, data4, data3"* Yes. As it says in [the answer](https://stackoverflow.com/questions/28994839/why-does-string-to-number-comparison-work-in-javascript/28994875#28994875) I [pointed you at before](https://stackoverflow.com/questions/66276804/sort-array-of-objects-javascript-by-values#comment117173774_66276804), `parseInt("")` returns `NaN`. If you want to handle them specially, you'll need to do that explicitly. For instance: – T.J. Crowder Feb 19 '21 at 17:42
  • 1
    `(a, b) => { const avalue = a.data.order === "" ? Infinity : parseInt(a.data.order); const bvalue = b.data.order === "" ? Infinity : parseInt(b.data.order); return a - b; }` or similar. – T.J. Crowder Feb 19 '21 at 17:43
  • @T.J.Crowder, `parseInt` is not necessary. – Nina Scholz Feb 19 '21 at 17:44
  • Hi! I guess you simply need to order values by `order` `data.sort((a, b) => { return (b.data.order !== '') - (a.data.order !== '') || a.data.order - b.data.order; });` – AdriSolid Feb 19 '21 at 17:53
  • Oh okay, sorry yes i meant empty string – amature Feb 19 '21 at 17:59
  • The main point is that im trying to firstly sorty by order if order is null sort by date_entered – amature Feb 19 '21 at 18:32
  • @AdriSolid I would like to sort by the order first and then if order is empty sort by date_entered – amature Feb 19 '21 at 18:42
  • @T.J.Crowder this works nicely but i would like to sort the date_entered by DESC :/ – amature Feb 19 '21 at 18:47
  • @T.J.Crowder `return ( avalue - bvalue || new Date(b.attributes.publication_date).getTime() - new Date(a.attributes.publication_date).getTime() );` @T.J.Crowder is this fine? – amature Feb 19 '21 at 18:57
  • @amature - That should do it, yes. :-) For *descending* you may want to swap those around, though: `bvalue - avalue`. – T.J. Crowder Feb 19 '21 at 19:04
  • 1
    @T.J.Crowder thanks a ton! – amature Feb 19 '21 at 19:05

0 Answers0