
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Sort Array of objects by two properties in JavaScript
Suppose, we have an array of objects like this −
const arr = [ { resVal: "25FA15", resFlow: 49, resName: "Rendimiento Tri−Seal Completo", resPhoto: "Tri−Sealseries.png", resHP: 1.5 }, { resVal: "25FA2", resFlow: 52, resName: "Rendimiento Tri−Seal Completo", resPhoto: "Tri−Sealseries.png", resHP: 2 }, { resVal: "45FA2", resFlow: 53, resName: "Rendimiento Hi−Cap Completo", resPhoto: "HighCapseries.png", resHP: 2 }, { resVal: "35FA2", resFlow: 59, resName: "Rendimiento Hi−Cap Completo", resPhoto: "HighCapseries.png", resHP: 2 } ];
We are required to write a JavaScript function that takes in one such array of objects. The function should sort this array based on two different properties −
sort by the higher "resFlow" value,
but with the lowest "resHP" value.
Approach
We are using a chained approach for a specified order of keys and their sort order.
The array is sorted by the properties −
resHP, ascending and
resFlow, descending.
It works with calculating the delta and this reflects the relation of the two objects. If the value is zero, then the two values are equal and the next delta is calculated and returned.
Example
The code for this will be −
const arr = [ { resVal: "25FA15", resFlow: 49, resName: "Rendimiento Tri−Seal Completo", resPhoto: "Tri−Sealseries.png", resHP: 1.5 }, { resVal: "25FA2", resFlow: 52, resName: "Rendimiento Tri−Seal Completo", resPhoto: "Tri−Sealseries.png", resHP: 2 }, { resVal: "45FA2", resFlow: 53, resName: "Rendimiento Hi−Cap Completo", resPhoto: "HighCapseries.png", resHP: 2 }, { resVal: "35FA2", resFlow: 59, resName: "Rendimiento Hi−Cap Completo", resPhoto: "HighCapseries.png", resHP: 2 } ]; const sortByTwo = (arr = []) => { arr.sort((a, b) => { return a.resHP − b.resHP || b.resFlow − a.resFlow; }); }; sortByTwo(arr); console.log(arr);
Output
And the output in the console will be −
[ { resVal: '25FA15', resFlow: 49, resName: 'Rendimiento Tri−Seal Completo', resPhoto: 'Tri−Sealseries.png', resHP: 1.5 }, { resVal: '35FA2', resFlow: 59, resName: 'Rendimiento Hi−Cap Completo', resPhoto: 'HighCapseries.png', resHP: 2 }, { resVal: '45FA2', resFlow: 53, resName: 'Rendimiento Hi−Cap Completo', resPhoto: 'HighCapseries.png', resHP: 2 }, { resVal: '25FA2', resFlow: 52, resName: 'Rendimiento Tri−Seal Completo', resPhoto: 'Tri−Sealseries.png', resHP: 2 } ]
- Related Articles
- Sort an array of objects by multiple properties in JavaScript
- Sort array of objects by string property value in JavaScript
- Sort array of objects by string property value - JavaScript
- Group values in array by two properties JavaScript
- JavaScript Sum of two objects with same properties
- Python Program to sort a tuple of custom objects by properties
- Map multiple properties in array of objects to the same array JavaScript
- How to merge properties of two JavaScript Objects dynamically?
- How to access properties of an array of objects in JavaScript?
- JavaScript Bubble sort for objects in an array
- Sort array based on presence of fields in objects JavaScript
- Filter array of objects whose properties contains a value in JavaScript
- How can I merge properties of two JavaScript objects dynamically?
- Sort by index of an array in JavaScript
- JavaScript: Sort Object of Objects

Advertisements