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; }

Or, using es6, simply:

data.sort((a, b) => a.city.localeCompare(b.city) || b.price - a.price);
Answer from Nina Scholz on Stack Overflow
🌐
TypeOfNaN
typeofnan.dev › sort-array-objects-multiple-properties-javascript
How to sort an array of objects by multiple properties in JavaScript | TypeOfNaN
March 20, 2023 - This actually ends up being fairly similar to our last example except this time we sort each element of the array using a property: const arr = [ { name: 'Mike', age: 20 }, { name: 'Peter', age: 30 }, { name: 'Doris', age: 18 }, ]; arr.sort((a, b) => a.age - b.age); console.log(arr); /* [ { name: 'Doris', age: 18 }, { name: 'Mike', age: 20 }, { name: 'Peter', age: 30 }, ] */ Finally, we have our goal case: sorting objects by multiple properties.
Discussions

Day 57: How Do You Sort an Array of Objects by Multiple Properties in JavaScript?
You can find an answer to this question and hundreds others on javascript-tips. No need to make an account on Medium. https://javascript-tips.weebly.com/array-tips-complete.html More on reddit.com
🌐 r/JavaScriptTips
1
0
August 2, 2025
Javascript - Sort Array of objects by 2 Properties - Stack Overflow
I want to sort an array of objects by 2 different properties. More on stackoverflow.com
🌐 stackoverflow.com
javascript - Sort an array of objects by multiple properties - Stack Overflow
-2 How to sort array in javascript or typescript by a key but if key is same sort in alphabetical order? 0 Sorting a JS Array of Objects on more than one key · 0 Sort objects by using three properties in javascript More on stackoverflow.com
🌐 stackoverflow.com
[Javascript] sort an array with multiple keys, breaking ties of previous key with next key
You can pass a comparator to the array.sort method. A comparator is a function that looks at two elements and returns 0 if they're equal, a negative number if the first element should come first, and a positive number if the second element should come first. You could express your sort logic the long way like this: stuff.sort((x,y) => { if (x.number < y.number) { return 1; } else if (x.number > y.number) { return -1; } else { if (x.name < y.name) {return 1; } else if (x.name > y.name) {return -1; } else { return 0; } } }) Once you understand how this works, you might want to make the code more concise. stuff.sort((x,y) => { if (x.number === y.number) { return y.name.localeCompare(x.name); } else { return y.number-x.number; } }) Since we're on the internet, I predict that somebody will reply with a shorter implementation. More on reddit.com
🌐 r/CodingHelp
4
1
April 1, 2022
🌐
freeCodeCamp
forum.freecodecamp.org › general
javaScript: sorting an array with multiple object/properties - General - The freeCodeCamp Forum
December 4, 2019 - Hello every one, I am trying to build a card game using javaScript. I have used the .sort() method to reorder my deck of cards. My array is composed by a value: int number and a string: the card’s suit.
🌐
Serhii Shramko
shramko.dev › home › snippets › sort an array of objects by multiple properties
How to Sort an Array of Objects by Multiple Properties in JavaScript
October 13, 2024 - Sorting an array of objects by multiple properties can be achieved using the Array.prototype.sort() method with a custom compare function.
🌐
Bithacker
bithacker.dev › javascript-object-multi-property-sort
Sorting multiple object properties with JavaScript | bit hacker
Thanks to the sort logic we can multiple the subtracted values by -1 to reverse the value of a and b. array.sort(function(a,b){ let val = 0; if( isNaN(a.fruit) && isNaN(b.fruit) ) { val = (a.fruit < b.fruit ? -1 : (a.fruit > b.fruit ? 1 : 0)); } else { val = a.fruit - b.fruit } return -1*val; }) Deeper: The actual sort algorithm used is dependent on JavaScript implementation and will vary from platform to platform. Sorting objects by a single property is great but we want a little more versatility.
🌐
TutorialsPoint
tutorialspoint.com › sort-an-array-of-objects-by-multiple-properties-in-javascript
Sort an array of objects by multiple properties in JavaScript
const arr = [ { id: 1, score: 1, isCut: false, dnf: false }, { id: 2, score: 2, isCut: false, dnf: false }, { id: 3, score: 3, isCut: false, dnf: false }, { id: 4, score: 4, isCut: false, dnf: false }, { id: 5, score: 5, isCut: true, dnf: true }, { id: 6, score: 6, isCut: true, dnf: false }, { id: 7, score: 7, isCut: true, dnf: false }, { id: 8, score: 8, isCut: true, dnf: false }, { id: 9, score: 9, isCut: true, dnf: false }, { id: 10, score: 0, isCut: false, dnf: false }, { id: 11, score: -1, isCut: false, dnf: false }, { id: 12, score: -2, isCut: false, dnf: true }, { id: 13, score: -3, isC
🌐
Reddit
reddit.com › r/javascripttips › day 57: how do you sort an array of objects by multiple properties in javascript?
r/JavaScriptTips on Reddit: Day 57: How Do You Sort an Array of Objects by Multiple Properties in JavaScript?
August 2, 2025 - https://javascript-tips.weebly.com/array-tips-complete.html · Search functions · r/DesignHomeGame • · upvotes · · comments · Did a little refactoring. r/Seablock • · 2 · upvotes · · comments · Data Structures in Python Visualized · r/datastructures • · upvotes · · comments · Day 47: Can You Sort an Array of Objects by a Property in JavaScript?
Top answer
1 of 4
50

You could use 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.

var array = [{ 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 }];

array.sort(function (a, b) {
    return a.resHP - b.resHP || b.resFlow - a.resFlow;
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 of 4
5

The requirement is to sort ascending by resHP property and then for any ties, sort descending by resFlow property.

Now, Javascript's Array.prototype.sort function allows us to sort an array. It accepts one parameter, which is a "compareFunction". The compareFunction accepts two parameters, which are elements of the array to be compared, and it must return their relative position in the expected resultant array.

function compareFunction (itemA, itemB) {...}

compareFunction(a, b) returns a numeric value greater than zero if b comes before a in the result.

compareFunction(a, b) returns a numeric value less than zero if b comes after a in the result.

compareFunction(a, b) returns zero if a and b are considered equivalent in terms of their relative order while sorting the array. For example, while sorting [5, 3, 5] both 5's are considered equivalent, hence the array should return 0 when compareFunction(5,5) is called.

For sorting strings, providing a compareFunction is not necessary. As per the specification, when compareFunction is not provided, each value in the array is converted to string, and then sorted. Here's an example:

console.log('["z", "ct", "a", "aa", "ab", "bc"].sort(): ', ["z", "ct", "a", "aa", "ab", "bc"].sort());

Common Mistakes:

Comparing numbers without compareFunction

Although compareFunction is optional by specification, if it is not provided while sorting numbers or other objects, it may result into incorrect results. Consider the example below:

console.log("[1, 2, 5, 3, 0, 20].sort(): ", [1, 2, 5, 3, 0, 20].sort());

Here 20 appears before 3 because each number is converted to string before comparison, and since "20" comes before "3" in dictionary (if there was such a dictionary) it appears above "3" in the result.

Returning boolean value from compareFunction instead of numeric value

This is my mistake in the original answer.

When you return a boolean value instead of a numeric one out of the compareFunction, the boolean value is cast into a number. Meaning true will become 1 and false will become 0, but you are not returning -1.

var array = [
  {
    compareThis: 1,
    originalIndex: 1
  },
  {
    compareThis: 2,
    originalIndex: 2
  },
  {
    compareThis: -1,
    originalIndex: 3
  },
  {
    compareThis: -2,
    originalIndex: 4
  }
];

console.log(array.sort((a, b) => a.compareThis > b.compareThis));

Here the result is the unsorted, same array because when 2 and -1 are compared, the comparison 2 > -1 returns false, which is converted to 0. But when compareFunction returns 0 it indicates that the order of the two elements shall remain unchanged. Hence the array doesn't get sorted.

The correct way to sort this array would be:

var array = [
  {
    compareThis: 1,
    originalIndex: 1
  },
  {
    compareThis: 2,
    originalIndex: 2
  },
  {
    compareThis: -1,
    originalIndex: 3
  },
  {
    compareThis: -2,
    originalIndex: 4
  }
];

console.log(array.sort((a, b) => a.compareThis - b.compareThis));


So with that, the correct way to solve the problem presented in the question would be:

var array = [{ 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 }];


// The requirement is to sort ascending by resHP property and then for any ties, sort descending by resFlow property.
/* array.sort(function(a, b) {
  if (a.resHP > b.resHP) {
    return 1;
  } else if (a.resHP === b.resHP) {
    if (a.resFlow > b.resFlow) {
      return -1;
    } else if (a.resFlow === b.resFlow) {
      return 0;
    } else {
      return 1;
    }
  } else {
    return -1;
  }
}); */

// Simplified to the following as we are working with numbers.
array.sort(function sortByResHP(a, b) {
  return (a.resHP - b.resHP) !== 0 ? (a.resHP - b.resHP) : (b.resFlow - a.resFlow);
});

console.log("Result: ", array);

Of course this can be further shortened as shown in the other answer. I am leaving the longer version of the compareFunction to make it easier to read.


Original (incorrect) answer

You could use Array#sort for this:

array = [{ 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 }]


array.sort(function(d1,d2) {
  if(d1.resHP == d2.resHP) {
    return d1.resFlow < d2.resFlow;
  } else {
    return d1.resHP > d2.resHP;
  }
});

console.log(array);
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › sort-array-of-objects-by-two-properties-in-javascript
Sort Array of objects by two properties in JavaScript
March 15, 2026 - // Define array of people const people = [ { name: 'Preeti', age: 25 }, { name: 'Aman', age: 30 }, { name: 'Shekhar', age: 20 }, { name: 'Preeti', age: 22 }, { name: 'Aman', age: 25 }, { name: 'Anjali', age: 28 } ]; const sortedPeople = people.sort((first, second) => { // First sort by name if (first.name < second.name) return -1; if (first.name > second.name) return 1; // If names are equal, sort by age if (first.age < second.age) return -1; if (first.age > second.age) return 1; return 0; // If both properties are equal }); console.log(sortedPeople); [ { name: 'Aman', age: 25 }, { name: 'Aman
🌐
Vultr Docs
docs.vultr.com › javascript › examples › sort array of objects by property values
JavaScript Program to Sort Array of Objects by Property ...
December 18, 2024 - Sorting arrays of objects by property values in JavaScript is a straightforward process once you master the use of the sort() method with appropriate comparator functions. From numeric and string properties to more complex multi-property sorting, ...
🌐
DEV Community
dev.to › calvin087 › sort-two-object-properties-with-arraysort-5d10
Sort two Object properties with Array.sort() - DEV Community
December 6, 2021 - We’re trying to return a positive, negative or 0 after checking the string order. So we use dot notation to get the value, compare them to each other and return a number to finish to original Array.sort() method. ... Pretending to be Human.
🌐
Ben Ilegbodu
benmvp.com › blog › quick-way-sort-javascript-array-multiple-fields
Quick way to sort a JavaScript array by multiple fields | Ben Ilegbodu
It's now our responsibility to tell the method which object should go first based on the number we return and .sort does the rest. Keep in mind. Even though .sort returns an array it actually mutates the source array by sorting in place.
🌐
Dev Tips
dev-tips.com › javascript › sort-array-by-multiple-properties-fields
Sort an array by multiple properties/fields in JavaScript
people.sort((a, b) => a.first_name.localeCompare(b.first_name) || a.last_name.localeCompare(b.last_name) || b.age - a.age, );
🌐
Go Make Things
gomakethings.com › sorting-an-array-by-multiple-criteria-with-vanilla-javascript
Sorting an array by multiple criteria with vanilla JavaScript | Go Make Things
You can also sort by additional criteria. If you had an array of objects with more than two properties, you could, for example, sort by a third criteria if the title property was the same between two items (maybe location or year).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-sort-an-array-of-object-by-two-fields-in-javascript
How to sort an array of object by two fields in JavaScript ? - GeeksforGeeks
June 26, 2024 - First compare the first property, if both are unequal then sort accordingly. If they are equal then do the same for the second property. Example: This example implements the above approach with a custom comparison function.
🌐
GitConnected
levelup.gitconnected.com › sort-array-of-objects-by-two-properties-in-javascript-69234fa6f474
Sort Array of Objects By Two Properties in JavaScript | by Code & Coins | Level Up Coding
March 13, 2023 - Sort Array of Objects By Two Properties in JavaScript One sort function, two sort orders I was recently working on a way to sort an array of objects alphabetically based on two columns. This means …
🌐
DEV Community
dev.to › markbdsouza › js-sort-an-array-of-objects-on-multiple-columns-keys-2bj1
JS: Sort an Array of Objects on multiple columns/keys - DEV Community
April 22, 2021 - That is if we want to sort by name first and then age · objs.sort((a,b)=> (a.name.localeCompare(b.name) || a.age - b.age)); ... Using the above logic, you can append how many ever sort columns you might need in the order you need them. ...
🌐
Reddit
reddit.com › r/codinghelp › [javascript] sort an array with multiple keys, breaking ties of previous key with next key
r/CodingHelp on Reddit: [Javascript] sort an array with multiple keys, breaking ties of previous key with next key
April 1, 2022 -

I have an array of objects with multiple properties:

eg:

```

[ 
	{
		name: "b",
		number: 1

	},
	{
		name: "a",
		number: 2

	},
	{
		name: "b",
		number: 2
	}
]

```

assume i want to sort by \[number, name\] descending, means that I want to sort by number in descending order first, which gives

```

[ 
	{
		name: "a",
		number: 2

	},
	{
		name: "b",
		number: 2
	},		
        {
		name: "b",
		number: 1
	},
]

```

then i resolve ties with the SAME number with descending order of name, which gives

```

[ 
	{
		name: "b",
		number: 2
	},
	{
		name: "a",
		number: 2
	},
        {
		name: "b",
		number: 1
	},
]

```