0

I am trying to sort an array of objects with reference to a key (name) and I want the data with uppercase to show first, but it is returning the data with lowercase letters first. I am using lodash method, orderby The array retrieved is :

data = [
   {
      "id":"00000000-0000-0000-0000-000000100000",
      "name":"DAS_Name_1",
      "layer":"Raw",
      "securityClass":"Green",
      "domainName":null,
      "domainId":null,
      "isActive":true,
      "isLocked":true,
      "creationDate":"2019-10-09T23:12:34Z",
      "createdByName":"DAS_Actor_User_Name_1",
      "createdById":"User_Id_1"
   },
   {
      "id":"00000000-0000-0000-0000-000000100009",
      "name":"u_123",
      "layer":"Standardized",
      "securityClass":"Green",
      "domainName":null,
      "domainId":null,
      "isActive":true,
      "isLocked":true,
      "creationDate":"2019-10-09T23:12:34Z",
      "createdByName":"DAS_Actor_User_Name_10",
      "createdById":"User_Id_10"
   },
   {
      "id":"00000000-0000-0000-0000-000000100099",
      "name":"Velvetica-123",
      "layer":"Standardized",
      "securityClass":"Red",
      "domainName":null,
      "domainId":null,
      "isActive":false,
      "isLocked":true,
      "creationDate":"2019-10-09T23:12:34Z",
      "createdByName":"DAS_Actor_User_Name_20",
      "createdById":"User_Id_20"
   },
   {
      "id":"00000000-0000-0000-0000-000000100100",
      "name":"test_run-2",
      "layer":"Data_Products",
      "securityClass":"Green",
      "domainName":null,
      "domainId":null,
      "isActive":true,
      "isLocked":true,
      "creationDate":"2019-10-09T23:12:34Z",
      "createdByName":"DAS_Actor_User_Name_1",
      "createdById":"User_Id_1"
   }
]

While I sort the data array with lodash method:

data = _.orderBy(data, ["name"], ["desc"]);

But I get the result as follows:

data = [
   {
      "id":"00000000-0000-0000-0000-000000100000",
      "name":"test_run-2",
      "layer":"Raw",
      "securityClass":"Green",
      "domainName":null,
      "domainId":null,
      "isActive":true,
      "isLocked":true,
      "creationDate":"2019-10-09T23:12:34Z",
      "createdByName":"DAS_Actor_User_Name_1",
      "createdById":"User_Id_1"
   },
   {
      "id":"00000000-0000-0000-0000-000000100009",
      "name":"u_123 ",
      "layer":"Standardized",
      "securityClass":"Green",
      "domainName":null,
      "domainId":null,
      "isActive":true,
      "isLocked":true,
      "creationDate":"2019-10-09T23:12:34Z",
      "createdByName":"DAS_Actor_User_Name_10",
      "createdById":"User_Id_10"
   },
   {
      "id":"00000000-0000-0000-0000-000000100099",
      "name":"Velvetica-123",
      "layer":"Standardized",
      "securityClass":"Red",
      "domainName":null,
      "domainId":null,
      "isActive":false,
      "isLocked":true,
      "creationDate":"2019-10-09T23:12:34Z",
      "createdByName":"DAS_Actor_User_Name_20",
      "createdById":"User_Id_20"
   },
   {
      "id":"00000000-0000-0000-0000-000000100100",
      "name":"DAS_Name_1 ",
      "layer":"Data_Products",
      "securityClass":"Green",
      "domainName":null,
      "domainId":null,
      "isActive":true,
      "isLocked":true,
      "creationDate":"2019-10-09T23:12:34Z",
      "createdByName":"DAS_Actor_User_Name_1",
      "createdById":"User_Id_1"
   }
]

which is incorrect. Can someone suggest what to do here?

1
  • Please edit your question Commented Jun 10, 2020 at 19:16

2 Answers 2

1

A simple ascending sort takes care of it.

As the ASCII value of "A" is 65 compared to the ASCII value of "a" which is 97.

So a simple ascending sort will be able to club all the Capitalised value to top of the array.

Tried it here

Hope this helps!

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

Comments

0

In Javascript, comparing strings using < or > is case sensitive [source], which means you could simple use Javascript's native Array.sort() function [documentation]:

data.sort(function(a, b) {
  if (a < b) {
    return -1
  }

  if (a > b) {
    return 1
  }

  return 0
})

Comments

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.