"javascript sort array by multiple properties" Code Answer's
You're definitely familiar with the best coding language TypeScript that developers use to develop their projects and they get all their queries like "javascript sort array by multiple properties" answered properly. Developers are finding an appropriate answer about javascript sort array by multiple properties related to the TypeScript coding language. By visiting this online portal developers get answers concerning TypeScript codes question like javascript sort array by multiple properties. Enter your desired code related query in the search bar and get every piece of information about TypeScript code related question on javascript sort array by multiple properties.
sort array of objects by 2 key value
homes.sort(
function(a, b) {
if (a.city === b.city) {
// Price is only important when cities are the same
return b.price - a.price;
}
return a.city > b.city ? 1 : -1;
});
Source: stackoverflow.com
javascript sort array by multiple properties
var sort_by;
(function() {
// utility functions
var default_cmp = function(a, b) {
if (a == b) return 0;
return a < b ? -1 : 1;
},
getCmpFunc = function(primer, reverse) {
var dfc = default_cmp, // closer in scope
cmp = default_cmp;
if (primer) {
cmp = function(a, b) {
return dfc(primer(a), primer(b));
};
}
if (reverse) {
return function(a, b) {
return -1 * cmp(a, b);
};
}
return cmp;
};
// actual implementation
sort_by = function() {
var fields = [],
n_fields = arguments.length,
field, name, reverse, cmp;
// preprocess sorting options
for (var i = 0; i < n_fields; i++) {
field = arguments[i];
if (typeof field === 'string') {
name = field;
cmp = default_cmp;
}
else {
name = field.name;
cmp = getCmpFunc(field.primer, field.reverse);
}
fields.push({
name: name,
cmp: cmp
});
}
// final comparison function
return function(A, B) {
var a, b, name, result;
for (var i = 0; i < n_fields; i++) {
result = 0;
field = fields[i];
name = field.name;
result = field.cmp(A[name], B[name]);
if (result !== 0) break;
}
return result;
}
}
}());
Source: stackoverflow.com
All those coders who are working on the TypeScript based application and are stuck on javascript sort array by multiple properties can get a collection of related answers to their query. Programmers need to enter their query on javascript sort array by multiple properties related to TypeScript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about javascript sort array by multiple properties for the programmers working on TypeScript code while coding their module. Coders are also allowed to rectify already present answers of javascript sort array by multiple properties while working on the TypeScript language code. Developers can add up suggestions if they deem fit any other answer relating to "javascript sort array by multiple properties". Visit this developer's friendly online web community, CodeProZone, and get your queries like javascript sort array by multiple properties resolved professionally and stay updated to the latest TypeScript updates.