0

I have an Array like this

let arr = [
    {label: "Bob Bobson", value: 17345, group: "Applicants"}
    {label: "Frank Frankson", value: 17347, group: "Applicants"}
    {label: "Bill Billson", value: 17348, group: "Applicants"}
    {label: "John Johnson", value: 1, group: "Caregivers"}
    {label: "Aaron Aaronson", value: 15, group: "Caregivers"}
    {label: "Jacob Jacobson", value: 172, group: "Clients"}
    {label: "Eric Ericsson", value: 1441, group: "Clients"}
    {label: "Doc Docson", value: 1147, group: "Doctors"}
]

I want to sort it using two of the property values in the object, the label property and the group property. I'm currently not getting any results however. I want to sort by group first and then label.

Here is what I am trying below.

arr.sort((a, b) => { a.label.toUpperCase() > b.label.toUpperCase()) ? 1 : -1 });

Using this I get the same results back, the label property doesn't get sorted.

Jino Shaji
  • 1,097
  • 14
  • 27
user3288619
  • 11
  • 1
  • 3

1 Answers1

-1

Your sort function isn't returning anything. Arrow functions can do an implicit return statement, but only if you have no curly brackets. So either remove the curly brackets, or add a return statement. Also, you have an extra closing parentheses after b.label.toUpperCase()

arr.sort((a, b) => a.label.toUpperCase() > b.label.toUpperCase() ? 1 : -1 );

arr.sort((a, b) => { 
    return a.label.toUpperCase() > b.label.toUpperCase() ? 1 : -1 
});
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98