javascript sort array of objects multiple fields

Solutions on MaxInterview for javascript sort array of objects multiple fields by the best coders in the world

showing results for - "javascript sort array of objects multiple fields"
Émilien
08 Jan 2021
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   });
Arianna
20 Nov 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
Sophie
27 Jan 2017
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
array sort by multiple values javascriptsort array of objects by 2 key valuejs objects array order by two keysjavascript sort based on two propertiessort array of objects by multiple fields in javajs object sort by multiple keysjavascript array sort two fieldsjs 2 array order by multiple fieldsjavascript sort criteriaorder array of objects by multiple propertiesjavascript sort based on multiple fieldssort array javascript on two propertiessort array of objects by two fieldsmultiple sort fields javascriptsort js array based on two property fieldsort array with multiple keys javascriptuse lodash to sort array of objects using multiple propertiessort by two fields jsjavascript multi compare sortsort two objects javascriptjavascript sort multiple fieldsts sort an array of object using two propertiesjs sort list of objects by key and than by valuejavascript sort based on 2 properties2 sort array by property javacriptsort objects by one keysort array two firstangular sort array of objects by multiple propertiesjavascript order by list multiple fieldssorting an array of objects with two fields in javascriptjavascript sort array alphabetically by multi propertiesjavascript sort on two propertiesjavascript sort array of nested objects by keyjs array sort on 2 propertiesmulti level sorting javascriptsort array by two properties jsjavascript sort an array of objects by multy propertyjavascript sort array of objects on multiple keysjs sort by multiple fields numbersort fields in object odejssort elements in arrayli by 2 elemts in javascriptjavascript array sort criteriaangular sort array by two propertiessort javascript object based on multiple fieldsjavascript sort by 2 fieldsjavascript sort on multiple fields sort objects with multiple key javascriptorder by multiple objects array javascriptjavascript sort array by 2 propertieshow to sort mulitple objects in javascriptjs array sort by multiple fieldsjavascript sort array by multiple keys in objectsort array objects based on multiple valuesjavascript sortby multiple fieldsjavascript sort by multiple propertiesjavascript sort array by two propertieshow to sort array with mutliple itemsjavascript sort array of objects multiple fieldssort json array by value with multi conditions javascriptjs sort array of objects with 2 valuessorting array by multiple values jsjavascript sort array of objects by two propertieshow to sort an array of objects by multiple fields 3f pythonsort array of objects based on array of objects fieldshow to sort by 2 properties javascriptjs sort array by multiple propertiessort array based on two properties if tiesort array javascript with 2 propertiesjavascript sort object by 2 valuestypescript sort array by two propertieshow to sort by more than one attribute in javascriptjs sort array of objects by two propertieshow to sort by multiple properties javascripttypescript useing array sort on multiple valuesjavascript sort array of objects by multiple propertiesangluar sort array by two propertiestypescript sort array of objects by multiple propertiesjavascript object array sort by multiple keyssort array of objects by two properties javascriptsort in javascript array of objects on two keysort by multiple properties javascriptsort array of objects by multiple propertiessort by multiple criteria javascriptsorting in array with multiple properties javascriptjavascript array object sort multiple fieldssort object by two key javascriptjavascript sort by two propertiessorting entire object in javascript with multiple valueshow to sort on two conditions javascriptin javascript how to sort by two valuessort an array of objects multiple criteria javasciptjavascript sort list of objects keep one keysort by multiple keys jssort array of objects based oupon keysort multiple attributes jssort multi criteria javascriptjavascript array sort multiple fieldshow to sort mulitple objects in single object javascriptsort by multiple fields javascriptarray sort by two propertiessort array by multiple values javascriptjavascript sort array on 2 valuesjs sort array of objects by 2 valuesjavascript how to order an array by multiple paramsjs sort two criteriajavascript sort array of objects by two keyshow to do multiple sort in an aaarray of objects in javascriptjavascript sort array of objects with same key valuehow to sort multiple objects in array javascriptmultiple sorting of objects in javascriptjavascript sort an array of objects by 2 keysjavascript sort by multiple fieldsjavascript multi sort array of objectssort object sorting for multiple propertiesmulitple sort an array of objects in javascriptjavascript sort array by multiple 28number 29 fieldssort array of objects javascript by two key valuesort by two key values node js json objectsjs object sort by multiple fieldsjavascript sort array of objects multiple keysjavascript sort array by two valuesort by two properties to one javascriptjavascript sort two conditionsjavascript sort multiple valuesjavascript sort array of objects two fieldsobject with multiple key sort based on propertysort array on two propertiesjavascript sort array multiple propertiesarray sort javascript object with two propertiesangular sort array two propertiesjavascript multiple sort criteriajavascript sort by 2 attributesjavascript array sort level 2 objectssorting with multiple keys in node jsjavascript sort array of objects by variable count of fieldsjs sort on two propertiesjs sort array for 2 attributeshow to sort based on two conditions in javascriptjs two criteria sortsort 2 array by field in side objectssorting objects by multiple properties javascriptjavascript sort multiple conditionsjavascript sort based on two properties of an array objecthow to sort based on two fields jsjavascript sort array of objects by property and mergesort array of objects by field value without mutatingfetch multiple fields from array of objects jsjs sort multiple fieldstypescript sort array of objects by multiple propertysort array of objects by multiple fields javasort using two properties javascriptjavascript sort json object by multiple propertieshow to sort in two condition jshow best to sort an object with two different property in javascriptjavascript sort array by multiple propertieshow to sort an array of objects by multiple fields 3fsort array multiple fields javascriptsort by 2 fields javascriptsort array of objects python multiple fieldssort js object by 2 propertiesjavascript sort array of object by multiple propertieshow to sort an array of objects by two fields in javascriptsort by two fields javascriptjavascript sort 2 propertiesjavascript sort complex array of objects by key namesort by multiple parameters ecmascriptjavascript sort array of objects by two fieldsjavascript sort object by 2 propertiesjavascript sort by two conditionsjs sort by two fieldsjavascript sort multiple propertiesjs sort array of objesc by multiplejavascript sort with two fieldssort array by two properties javascripttypescript two property sort criteriasort multiple key object based on property valuehow to sort an object with multiple fieldsorder array of object using two keyjavascript sort object by two propertiessort object array with multiple fiueldssort based on two conditions javascriptmerge two array of objects based on a keysort array by 2 propertiessort object with multiple keysort array of objects by multiple properties javascriptcomparator sort multiple in javascriptjs array sort by two fieldssort by multiple fields jssort array on multiple fieldsplain js sort array of objects multiple levelsjavascript sort by several fieldshow to use sort with two criterias jsjavascript sort by two fieldsjavascript array sort by two propertiesjs sort multiple propertiesjquery sort array object by 2 columnsjavascript sort array of objects by multiple key valuesort on multiple fields javascriptjs sort by two thingssort by multiple attributes javascriptes6 sort array of objects by multiple propertiesjs sort array of objects by 2 fieldssort by property 2 array javascriptjavascript sort array multiple criteriasort 2 array of objects javascriptjavascript sort array of objects multiple levelsjs sort array of object by multiplehow to sort multiple properties javascriptjavascript sort multidimensional array by property namesort array by two fields javascriptsort array by 2 other propertiesjavascript array sort by 2 fieldsjavascript sort multy type data arrayjavascript sort array multiple fieldsjs sort by multiple fieldshow to sort js array of objects based on two attributessort by two properties javascriptjavascript sort array by 2 valuessort an array of objects in javascript by a single key javascript sort and group objects with same key valuejs sort by 2 propertiessort method based on multiple elements in javascriptjavascript sort array of objects by 2 propertiesjavascript sort array of objects by multiple keyscompare objects with multiple properties typescriptjava sort object by multiple fieldssort list by 3 fields in javascriptjavascript sort object array by two propertiessorting an array by multiple properties in javascriptsort array by two propertiesjs sort by two propertiesjs sort multiple criteriasort object multiple key propertyjavascript sort by 2 propertieshow to sort an array holding two fields in javascriptjs sort by multiple propertieshow to sort multiple objects in a hash javascriptjs sort 2 fieldsjs sort on multiple object properties namejavascript sort json array by multiple valueshow to sort 2 different objects based on key valuejavascript sort array objecys multiple fieldsjavascript sort array of objects multiple fields