Lulunga

Sort an Array by 2 Criteria

Jun 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. function solve(input) {
  2. let sorted = input.sort(sortedNames);
  3. console.log(sorted.join('\n'));
  4.  
  5. function sortedNames(a, b) {
  6. let firstCriteria = a.length - b.length;
  7.  
  8. if (firstCriteria === 0) {
  9. return a.localeCompare(b);
  10. }
  11. return firstCriteria;
  12. }
  13. }
  14. /*Write a function that orders an array of strings, by their length in ascending order as primary criteria, and by alphabetical value in ascending order as second criteria. The comparison should be case-insensitive.*/
Advertisement
Add Comment
Please, Sign In to add comment