Skip to content
Advertisement

Javascript sort function. Sort by First then by Second

I have an array of objects to sort. Each object has two parameters: Strength and Name

JavaScript

I want to sort first by Strength and then by name alphabetically. I am using the following code to sort by the first parameter. How do I sort then by the second?

JavaScript

Thanks for your help.

(I am using Array.sort() with the aforementioned sortF as the sort comparison function passed into it.)

Advertisement

Answer

Expand your sort function to be like this;

JavaScript

A < and > comparison on strings is an alphabetic comparison.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement