sort Javascript array by two numeric fields
grouperArray.sort(function (a, b) {
var aSize = a.gsize;
var bSize = b.gsize;
var aLow = a.glow;
var bLow = b.glow;
console.log(aLow + " | " + bLow);
if(aSize == bSize)
{
return (aLow < bLow) ? -1 : (aLow > bLow) ? 1 : 0;
}
else
{
return (aSize < bSize) ? -1 : 1;
}
});
How to sort an array of objects by multiple fields?
You could use a chained sorting approach by taking the delta of values until it reaches a value not equal to zero.
var data = [{ h_id: "3", city: "Dallas", state: "TX", zip: "75201", price: "162500" }, { h_id: "4", city: "Bevery Hills", state: "CA", zip: "90210", price: "319250" }, { h_id: "6", city: "Dallas", state: "TX", zip: "75000", price: "556699" }, { h_id: "5", city: "New York", state: "NY", zip: "00010", price: "962500" }];
data.sort(function (a, b) { return a.city.localeCompare(b.city) || b.price - a.price;});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
How to sort different types of data according to their priority?
Arrange the comparison test to be lexicographical, first on the type (assigning priority indexes such as like number: 0, string: 1), then on the value in case of ties. This is clean and extensible.
To retain the order, you need to use a stable sorting algorithm such as MergeSort.
Alternatively, if you don't plan to add other types, proceed as follows:
scan the list and split (stably) in a sublist with the numbers and another sublist with the strings;
sort the sublists and concatenete them.
Sorting by two values in python. One ascending and one descending
Use:
data = {'david': ['APP', 2], 'ala': ['PROD', 2], 'steven': ['DEV', 4], 'katya': ['SYS', 2]}
res = sorted(data.items(), key=lambda x: (x[1][1] * -1, x[1][0]))
print(res)
Output
[('steven', ['DEV', 4]), ('david', ['APP', 2]), ('ala', ['PROD', 2]), ('katya', ['SYS', 2])]
Multiplying by -1
will make the sorting descending on the second element.
Javascript Sort array of objects by value returned by calling method
I think I figured it out:
function reorder(){
var pinnedTasksPos = []
var pinnedTasksNeg = []
var regularTasksPos = []
var regularTasksNeg = []
var finishedTasksPos = []
var finishedTasksNeg = []
for (e=0; e<tasks.length; e++){
let i = tasks[e]
console.log(i)
if(i.pinned){
if(i.dueDaysDiff >= 0) {
pinnedTasksPos.push(i)
} else{
pinnedTasksNeg.push(i)
}
} else if(i.done){
if(i.dueDaysDiff >= 0) {
finishedTasksPos.push(i)
} else{
finishedTasksNeg.push(i)
}
} else{
if(i.dueDaysDiff >= 0) {
regularTasksPos.push(i)
} else{
regularTasksNeg.push(i)
}
}
}
pinnedTasksPos.sort(compare)
pinnedTasksNeg.sort(compare).reverse()
regularTasksPos.sort(compare)
regularTasksNeg.sort(compare).reverse()
finishedTasksPos.sort(compare)
finishedTasksNeg.sort(compare).reverse()
var newarr = [...pinnedTasksPos, ...pinnedTasksNeg, ...regularTasksPos, ...regularTasksNeg, ...finishedTasksPos, ...finishedTasksNeg]
return(newarr.reverse())
}
function compare(a, b){
if(a.dueDaysDiff < b.dueDaysDiff){
return -1
}
if(a.dueDaysDiff > b.dueDaysDiff){
return 1
}
return 0
}
Related Topics
Merging or Combining Two Onedit Trigger Functions
Differencebetween Bower and Npm
Firebase: Setting Additional User Properties
Understanding Prototypal Inheritance in JavaScript
What Does the @ Mean Inside an Import Path
How to Create a Jquery Plugin with Methods
Invoking a Function Without Parentheses
Best Way to Add Page Specific JavaScript in a Rails 3 App
How to Get Elements with Multiple Classes
Is There a Built-In Way to Loop Through the Properties of an Object
Check If All Values of Array Are Equal
One Liner to Flatten Nested Object
Disable Submit Button on Form Submit
Template String as Object Property Name
Ajax Mailchimp Signup Form Integration
How to Guarantee That My Enums Definition Doesn't Change in JavaScript