Answers for "sort nested data using sort function javascript"

0

sort nested data using sort function javascript

var sort = function (prop, arr) {
    prop = prop.split('.');
    var len = prop.length;

    arr.sort(function (a, b) {
        var i = 0;
        while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
        if (a < b) {
            return -1;
        } else if (a > b) {
            return 1;
        } else {
            return 0;
        }
    });
    return arr;
};
Posted by: Guest on June-05-2020

Code answers related to "sort nested data using sort function javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language