sort nested data using sort function javascript

Solutions on MaxInterview for sort nested data using sort function javascript by the best coders in the world

showing results for - "sort nested data using sort function javascript"
Alessandro
09 Mar 2017
1var sort = function (prop, arr) {
2    prop = prop.split('.');
3    var len = prop.length;
4
5    arr.sort(function (a, b) {
6        var i = 0;
7        while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
8        if (a < b) {
9            return -1;
10        } else if (a > b) {
11            return 1;
12        } else {
13            return 0;
14        }
15    });
16    return arr;
17};