2

I got an array of object

const users= [{
Location: {
  label: "aa",
  value: "aa"
},
Name: "test7"
id: "002"
},
Location: {
  label: "aa",
  value: "aa"
},
Name: "test4"
id: "003"
},

{
Location: {
  label: "ss",
  value: "ss"
},
Name: "test4"     
id: "004"
}]

I want to sort the array by first using Location.lable then using Name (Ascending order)

Here is what I have already done and it doesn't work

const dynamicSort = property => {
var sortOrder = 1;
if (property[0] === "-") {
  sortOrder = -1;
  property = property.substr(1);
}
return function(a, b) {
  var result;
  if (property === "deviceLocation")
    result =
      a[property].label < b[property].label
        ? -1
        : a[property].label > b[property].label
        ? 1
        : 0;
  else
    result =
      a[property] < b[property] ? -1 : a[property] > b[property] ? 1 : 0;
  return result * sortOrder;
};
};
users.sort(dynamicSort("deviceLocation"))

the result should be like this :

const users= [{
  Location: {
  label: "aa",
  value: "aa"
},
Name: "test4"
id: "003"
},

Location: {
  label: "aa",
  value: "aa"
},
Name: "test7"
id: "002"
},

{
Location: {
  label: "ss",
  value: "ss"
},
Name: "test4"     
id: "004"
}]

how to sort the array object first using Location.label and then with Name. i tried lodash _.groupBy and after that sort but it didn't work

2
  • Too lazy to write formatted answer: users.sort((a, b) => a.Location.label === b.Location.label ? a.Name > b.Name ? 1 : -1 : a.Location.label > b.Location.label ? 1 : -1) Commented Feb 28, 2020 at 17:03
  • Please don't paste code with syntax errors to stackoverflow, it's kinda disrespect Commented Feb 28, 2020 at 17:04

1 Answer 1

2

First, you should use JSON or the JavaScript object in your question.

Then, you can create a sort function that first sorts the label of the user's location, followed by the name of the user.

console.log(getUsers().sort(userComparator));

function userComparator(userA, userB) {
  let diff = userA.Location.label.localeCompare(userB.Location.label);
  return diff === 0 ? userA.Name.localeCompare(userB.Name) : diff;
}

function getUsers() {
  return [{
    "Location": {
      "label": "colombo",
      "value": "colombo"
    },
    "Name": "test7",
    "id": "002"
  }, {
    "Location": {
      "label": "jaffna",
      "value": "jaffna"
    },
    "Name": "test4",
    "id": "004"
  }, {
    "Location": {
      "label": "colombo",
      "value": "colombo"
    },
    "Name": "test4",
    "id": "003"
  }];
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

Sign up to request clarification or add additional context in comments.

2 Comments

I think you can just do return userA.Location.label.localeCompare(userB.Location.label) || userA.Name.localeCompare(userB.Name)
@Prasanna You could, but the short-circuit || hides the x === 0 meaning. I just wanted to be a little more verbose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.