how to sort an array by value in javascript
//ascending order
arr.sort(function(a, b){return a-b});
arr.sort((a, b) => a - b);
//descending order
arr.sort(function(a, b){return b - a});
arr.sort((a, b) => b - a);
how to sort an array by value in javascript
//ascending order
arr.sort(function(a, b){return a-b});
arr.sort((a, b) => a - b);
//descending order
arr.sort(function(a, b){return b - a});
arr.sort((a, b) => b - a);
javascript sort object
var maxSpeed = {
car: 300,
bike: 60,
motorbike: 200,
airplane: 1000,
helicopter: 400,
rocket: 28800
};
var sortable = [];
for (var vehicle in maxSpeed) {
sortable.push([vehicle, maxSpeed[vehicle]]);
}
sortable.sort(function(a, b) {
return a[1] - b[1];
});
//[["bike", 60], ["motorbike", 200], ["car", 300],
//["helicopter", 400], ["airplane", 1000], ["rocket", 28800]]
javascript sort
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us