0

I am trying to sort this array:

[
  {
    "id": 877234004,
    "name": "app.js",
    "type": "FILE",
    "parentId": 877234003
  },
  {
    "id": 877234010,
    "name": "project",
    "type": "DIRECTORY"
  },
  {
    "id": 877234002,
    "name": "src",
    "type": "DIRECTORY",
    "parentId": 877234010
  },
  {
    "id": 877234003,
    "name": "app",
    "type": "DIRECTORY",
    "parentId": 877234002
  }
]

As you can see, there are object who doesn't have parentId. I need to keep this one on the top and the rest will be sorted by id.

Here's my code:

input.sort(function (a, b) {
    if (typeof a.parentId === 'undefined')) 
      return -1;
    else
      return a.id - b.id;
  });

It doesn't work though. Is it possible to do this?

Expected result:

[
  {
    "id": 877234010,
    "name": "project",
    "type": "DIRECTORY"
  },
  {
    "id": 877234002,
    "name": "src",
    "type": "DIRECTORY",
    "parentId": 877234010
  },
  {
    "id": 877234003,
    "name": "app",
    "type": "DIRECTORY",
    "parentId": 877234002
  },
  {
    "id": 877234004,
    "name": "app.js",
    "type": "FILE",
    "parentId": 877234003
  }
]

Thanks in advance

1
  • if sorting in descending order then in expected result parentId 877234003 will come first then parentId 877234003. pls edit your question. Commented Jan 2, 2019 at 5:32

2 Answers 2

2

i would suggest to use lodash js library

var arr = [
  {
    "id": 877234004,
    "name": "app.js",
    "type": "FILE",
    "parentId": 877234003
  },
  {
    "id": 877234010,
    "name": "project",
    "type": "DIRECTORY"
  },
  {
    "id": 877234002,
    "name": "src",
    "type": "DIRECTORY",
    "parentId": 877234010
  },
  {
    "id": 877234003,
    "name": "app",
    "type": "DIRECTORY",
    "parentId": 877234002
  }
];

var sortedArr = _.orderBy(arr, 'parentId', 'desc');

console.log(sortedArr);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

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

Comments

1

Need to test A and B and invert. if A is missing the prop use -1 if B use +1

let arr = [{
    "id": 877234004,
    "name": "app.js",
    "type": "FILE",
    "parentId": 877234003
  },
  {
    "id": 877234010,
    "name": "project",
    "type": "DIRECTORY"
  },
  {
    "id": 877234002,
    "name": "src",
    "type": "DIRECTORY",
    "parentId": 877234010
  },
  {
    "id": 877234003,
    "name": "app",
    "type": "DIRECTORY",
    "parentId": 877234002
  }
];

const sortPredicate = (a,b) => {
  if (a.hasOwnProperty('parentId') === false) {
    return -1;
  } else if(b.hasOwnProperty('parentId') === false) {
    return 1;
  } else {
    return a.id - b.id;
  }
};


console.log(arr.sort(sortPredicate));

1 Comment

An possible optimization would be to return the difference between each hasOwnProperty check.

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.