Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compound sorting(ordering) of object array [duplicate]

I have an array of objects similar in design as listed below. I would like to sort this array first by by the ClassName (alphabetically), then by the StartDate, but I want to preserve the initial sorting. I understand I can likely use sort() to accomplish this but I'm not sure how to preserve the initial sorting w/o breaking the original array into smaller groupings.

var objArray = [
    {
        ClassName: "Excel",
        Location: "Kansas City",
        StartDate: "2/1/2016",
        EndDate: "6/2/2016,"
    },
    {
        ClassName: "Outlook",
        Location: "Kansas City",
        StartDate: "1/1/2016",
        EndDate: "5/2/2016,"
    },
    {
        ClassName: "Excel",
        Location: "Kansas City",
        StartDate: "3/1/2016",
        EndDate: "7/2/2016,"
    }
];

Ideally based on the data above I'd end up with something like this:

var objArray = [
    {
        ClassName: "Excel",
        Location: "Kansas City",
        StartDate: "2/1/2016",
        EndDate: "6/2/2016,"
    },
    {
        ClassName: "Excel",
        Location: "Kansas City",
        StartDate: "3/1/2016",
        EndDate: "7/2/2016,"
    },
    {
        ClassName: "Outlook",
        Location: "Kansas City",
        StartDate: "1/1/2016",
        EndDate: "5/2/2016,"
    }
];

and again for clarity, if there were multiple classes the final sorting would end up looking something like this.

Excel (1/1/2016)
Excel (1/2/2016)
Excel (2/3/2016)
Outlook (1/3/2016)
Outlook (2/3/2016)
Word (1/1/2016)
Word (5/5/2016)
like image 770
Tekk_Know Avatar asked Aug 06 '26 10:08

Tekk_Know


1 Answers

The link @emed pointed to for possible duplicate, had an interesting solution.

My only issue with it would have been performance, as he's doing map's and reduce constantly inside the compare function.

So I've done a slightly modified version, and I've also kept the ability to do reverse sorting.

var objArray = [
    {
        ClassName: "Excel",
        Location: "Kansas City",
        StartDate: "2/1/2016",
        EndDate: "6/2/2016,"
    },
    {
        ClassName: "Outlook",
        Location: "Kansas City",
        StartDate: "1/1/2016",
        EndDate: "5/2/2016,"
    },
    {
        ClassName: "Excel",
        Location: "Kansas City",
        StartDate: "3/1/2016",
        EndDate: "7/2/2016,"
    }
];

objArray.sort(fieldSorter(['ClassName', 'StartDate']));
console.log(objArray);

function fieldSorter(fields) {
    var maps = [];
    fields.map(function (o) {
       var dir = +1;
       if (o[0] === '-') {
          dir = -1;
          o=o.substring(1);
       }
       maps.push({fn:o, dir:dir});
    });
    return function (a, b) {
       var ret = 0;
       maps.some(function (o) {
         if (a[o.fn] > b[o.fn]) ret = o.dir;
         else if (a[o.fn] < b[o.fn]) ret = -o.dir;
         else ret = 0;
         return ret !== 0;
       });
       return ret;
    };
}
like image 109
Keith Avatar answered Aug 08 '26 23:08

Keith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!