javascript sort array by multiple properties

Solutions on MaxInterview for javascript sort array by multiple properties by the best coders in the world

showing results for - "javascript sort array by multiple properties"
Sofia
16 Jul 2017
1homes.sort(
2   function(a, b) {          
3      if (a.city === b.city) {
4         // Price is only important when cities are the same
5         return b.price - a.price;
6      }
7      return a.city > b.city ? 1 : -1;
8   });
Ariadna
04 May 2019
1array.sort(function(a,b){
2  let i = 0, result = 0;
3  while(i < sortBy.length && result === 0) {
4    result = sortBy[i].direction*(a[ sortBy[i].prop ].toString() < b[ sortBy[i].prop ].toString() ? -1 : (a[ sortBy[i].prop ].toString() > b[ sortBy[i].prop ].toString() ? 1 : 0));
5    i++;
6  }
7  return result;
8})
9
Imen
29 Jan 2020
1var sort_by;
2
3(function() {
4    // utility functions
5    var default_cmp = function(a, b) {
6            if (a == b) return 0;
7            return a < b ? -1 : 1;
8        },
9        getCmpFunc = function(primer, reverse) {
10            var dfc = default_cmp, // closer in scope
11                cmp = default_cmp;
12            if (primer) {
13                cmp = function(a, b) {
14                    return dfc(primer(a), primer(b));
15                };
16            }
17            if (reverse) {
18                return function(a, b) {
19                    return -1 * cmp(a, b);
20                };
21            }
22            return cmp;
23        };
24
25    // actual implementation
26    sort_by = function() {
27        var fields = [],
28            n_fields = arguments.length,
29            field, name, reverse, cmp;
30
31        // preprocess sorting options
32        for (var i = 0; i < n_fields; i++) {
33            field = arguments[i];
34            if (typeof field === 'string') {
35                name = field;
36                cmp = default_cmp;
37            }
38            else {
39                name = field.name;
40                cmp = getCmpFunc(field.primer, field.reverse);
41            }
42            fields.push({
43                name: name,
44                cmp: cmp
45            });
46        }
47
48        // final comparison function
49        return function(A, B) {
50            var a, b, name, result;
51            for (var i = 0; i < n_fields; i++) {
52                result = 0;
53                field = fields[i];
54                name = field.name;
55
56                result = field.cmp(A[name], B[name]);
57                if (result !== 0) break;
58            }
59            return result;
60        }
61    }
62}());
63
queries leading to this page
sort list by 3 fields in javascriptsort array of objects javascript by two key valuejavascript sort by two conditionsjs sort multiple propertiesjavascript sort based on multiple fieldsorder by multiple objects array javascript2 sort array by property javacriptjavascript sort criteriatypescript sort array of objects by multiple propertyangular sort array by two propertiesjavascript sort by multiple propertieshow to sort an array of objects by multiple fields 3f pythonsort 2 array of objects javascriptsort js object by 2 propertiessort object sorting for multiple propertiesmultiple sorting of objects in javascriptjavascript sort by two fieldssort array objects based on multiple valuesjavascript sort by 2 fieldsjavascript sort multiple fieldssort multiple attributes jsjavascript multi compare sorthow to sort on two conditions javascriptjs sort array of objects with 2 valuesjavascript sort json object by multiple propertiessorting array by multiple values jssort array by two fields javascripthow to sort based on two conditions in javascriptsort array multiple fields javascriptts sort an array of object using two propertiessort two objects javascriptjavascript sort by several fieldssort by multiple fields jssort array of objects by multiple fields in javajavascript multiple sort criteriajs sort array by multiple propertiessort array with multiple keys javascriptarray sort by two propertiesjavascript sort and group objects with same key valuejavascript sort array of objects by two propertiesjs sort on two propertiessort multi criteria javascripttypescript useing array sort on multiple valuesjs sort array of objects by 2 fieldsjs sort array of objects by 2 valuesorder array of objects by multiple propertieshow to sort 2 different objects based on key valuesorting entire object in javascript with multiple valuessort by multiple criteria javascriptjavascript sort array of objects by multiple keyssort javascript object based on multiple fieldsjavascript sort object by two propertiesjavascript sort object by 2 valueshow to sort by multiple properties javascriptsort array of objects python multiple fieldssort array of objects by 2 key valuesort object array with multiple fiueldsjavascript sort list of objects keep one keyjavascript sort by 2 propertiessort object multiple key propertysort an array of objects multiple criteria javascipthow to sort mulitple objects in javascripthow to sort by 2 properties javascriptjavascript sort with two fieldsjavascript sort array of objects by 2 propertiesjavascript sort objects by multiple propertiessort by 2 fields javascriptjavascript sort array of objects on multiple keyssort by multiple parameters ecmascriptsort by multiple fields javascriptmulti level sorting javascriptjavascript sort multiple valuesjavascript sort array of objects by property and mergehow best to sort an object with two different property in javascripthow to do multiple sort in an aaarray of objects in javascriptuse lodash to sort array of objects using multiple propertiesjavascript sort based on two propertiesjavascript sort based on two properties of an array objecthow to sort js array of objects based on two attributeshow to sort an array of objects by multiple fields 3fjavascript array sort multiple fieldsfetch multiple fields from array of objects jssort by two fields jsjavascript object array sort by multiple keyssort json array by value with multi conditions javascriptjquery sort array object by 2 columnssort object with multiple keysort array by two properties jsjavascript sort object by 2 propertiesjavascript sort array by multiple propertieshow to sort an object with multiple fieldsjavascript sort multidimensional array by property namejs sort array of objects by two propertiesjavascript sort an array of objects by multy propertyjs sort by 2 propertiesmulitple sort an array of objects in javascriptsort an array of objects in javascript by a single key array sort javascript object with two propertiesjs sort by two thingstypescript sort array of objects by multiple propertiesjs sort by multiple fieldsjs 2 array order by multiple fieldsjavascript sort on multiple fieldstypescript two property sort criteriamerge two array of objects based on a keyarray sort by multiple values javascriptjava sort object by multiple fieldsjavascript array sort two fieldshow to sort an array holding two fields in javascripthow to sort an array of objects by two fields in javascriptjavascript sort by multiple fieldsjavascript sort array of objects by two keysjavascript sort array of objects multiple keyssort by multiple attributes javascriptjs two criteria sorthow to sort array with mutliple itemssort elements in arrayli by 2 elemts in javascriptplain js sort array of objects multiple levelsjavascript sort array by 2 propertiesjavascript array sort level 2 objectsjavascript order by list multiple fieldsjavascript sort multy type data arrayjavascript sort multiple conditionssorting in array with multiple properties javascriptjs object sort by multiple fieldsjs sort 2 fieldsjavascript sort complex array of objects by key namejavascript sort by two propertiessort array javascript on two propertiesjs sort multiple fieldsangular sort array two propertiessorting objects by multiple properties javascriptsort by multiple keys jsjavascript sortby multiple fieldsjavascript sort an array of objects by 2 keysjavascript array sort by 2 fieldsjs sort by two fieldsjavascript sort two conditionsjs sort two criteriasort array by two propertieshow to sort based on two fields jsjavascript sort array objecys multiple fieldssort 2 array by field in side objectssort array by 2 propertiesjavascript sort array of objects by variable count of fieldssort array on two propertieses6 sort array of objects by multiple propertiessort array on multiple fieldssort array of objects based on array of objects fieldssort array of objects by field value without mutatingjavascript sort array of objects by multiple key valuesort array by multiple values javascriptorder array of object using two keysort in javascript array of objects on two keyjavascript sort array multiple criteriasort by multiple properties javascriptjavascript sort by 2 attributesjavascript sort 2 propertiessorting an array of objects with two fields in javascriptcomparator sort multiple in javascripthow to sort multiple properties javascriptsort method based on multiple elements in javascriptjs objects array order by two keysjavascript multi sort array of objectsjavascript array object sort multiple fieldsjs sort by multiple propertiesjavascript array sort criteriaangular sort array of objects by multiple propertiesjavascript sort array multiple fieldssort multiple key object based on property valuejavascript sort array of objects multiple levelsjs object sort by multiple keyssorting an array by multiple properties in javascriptjavascript sort based on 2 propertieshow to use sort with two criterias jsjavascript sort array by multiple 28number 29 fieldssort by two properties to one javascriptsort object by two key javascriptjavascript sort array by two propertiescompare objects with multiple properties typescripthow to sort by more than one attribute in javascripthow to sort mulitple objects in single object javascripttypescript sort array by two propertiesjavascript sort on two propertiessort array two firstjavascript sort array multiple propertiesjs array sort on 2 propertiessort fields in object odejsjs array sort by multiple fieldshow to sort multiple objects in a hash javascriptsorting with multiple keys in node jssort array of objects by two properties javascriptjs array sort by two fieldssort array of objects by multiple properties javascriptsort by property 2 array javascriptsort by two fields javascriptjavascript how to order an array by multiple paramssort array of objects by multiple fields javajavascript sort array of object by multiple propertiesjavascript sort array of objects by two fieldsjs sort multiple criteriahow to sort multiple objects in array javascriptsort array javascript with 2 propertiesangluar sort array by two propertiesjavascript array sort by two propertiesjs sort array of objesc by multiplejavascript sort json array by multiple valuesjavascript sort array on 2 valueshow to sort in two condition jsjavascript sort array by multiple keys in objectsort using two properties javascriptsort by two key values node js json objectssort array based on two properties if tiejavascript sort array by 2 valuesjs sort list of objects by key and than by valuein javascript how to sort by two valuessort by two properties javascriptsort array by 2 other propertiesjavascript sort multiple propertiessort js array based on two property fieldjavascript sort object array by two propertiessort objects by one keyjs sort array for 2 attributessort based on two conditions javascriptobject with multiple key sort based on propertyjavascript sort array alphabetically by multi properties sort objects with multiple key javascriptjs sort by multiple fields numberjs sort on multiple object properties namemultiple sort fields javascriptjavascript sort array of objects two fieldsjs sort array of object by multiplejavascript sort array of objects with same key valuejavascript sort array of nested objects by keyjavascript sort array of objects by multiple propertiessort array of objects by multiple propertiesjavascript sort array of objects multiple fieldssort array of objects by two fieldssort array by two properties javascriptsort on multiple fields javascriptsort array of objects based oupon keyjavascript sort array by two valuejs sort by two propertiesjavascript sort array by multiple properties