0

Want to sort my data all node come first before calling its parent node

[
  {
    "id": 7832454551,
    "name": "usr",
    "type": "DIRECTORY"
  },
  {
    "id": 7832454554,
    "name": "applications",
    "type": "DIRECTORY",
    "parentId": 7832454553
  },
  {
    "id": 7832454555,
    "name": "mimeinfo.cache",
    "type": "FILE",
    "parentId": 7832454554
  },
  {
    "id": 7832454553,
    "name": "share",
    "type": "DIRECTORY",
    "parentId": 7832454552
  },
  {
    "id": 7832454552,
    "name": "local",
    "type": "DIRECTORY",
    "parentId": 7832454551
  }
]

want to update data like this using javascript

[
  {
    "id": 7832454551,
    "name": "usr",
    "type": "DIRECTORY"
  },
  {
    "id": 7832454552,
    "name": "local",
    "type": "DIRECTORY",
    "parentId": 7832454551
  },
  {
    "id": 7832454553,
    "name": "share",
    "type": "DIRECTORY",
    "parentId": 7832454552
  },
  {
    "id": 7832454554,
    "name": "applications",
    "type": "DIRECTORY",
    "parentId": 7832454553
  },
  {
    "id": 7832454555,
    "name": "mimeinfo.cache",
    "type": "FILE",
    "parentId": 7832454554
  }
]
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • 1
    Hi Hajaj, Welcome to stackoverflow. Can you show us your efforts What code you tried to solve this issue? What challenge or error you faced when you tried to solve? – Prasad Telkikar Jun 21 '20 at 08:00
  • Thanks, want to make a folder structure using there type and name – Hajaj Khan Jun 21 '20 at 08:03
  • Welcome to Stack Overflow! Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Jun 21 '20 at 08:06
  • there is a parent id that's contain another node id. when we pick data one by one if parent-id node not exist then found error. i just want to sort my data before starting making nods. – Hajaj Khan Jun 21 '20 at 08:06

1 Answers1

0

try this one

const sortedData = [
  {
    "id": 7832454551,
    "name": "usr",
    "type": "DIRECTORY"
  },
  {
    "id": 7832454554,
    "name": "applications",
    "type": "DIRECTORY",
    "parentId": 7832454553
  },
  {
    "id": 7832454555,
    "name": "mimeinfo.cache",
    "type": "FILE",
    "parentId": 7832454554
  },
  {
    "id": 7832454553,
    "name": "share",
    "type": "DIRECTORY",
    "parentId": 7832454552
  },
  {
    "id": 7832454552,
    "name": "local",
    "type": "DIRECTORY",
    "parentId": 7832454551
  }
].sort(function(a,b){

if(a.parentId!= undefined && b.parentId != undefined){
  return a.parentId > b.parentId
}
return false;


});

console.log(sortedData);

newbie
  • 128
  • 2
  • 15
  • no its working ur code make data like this [ { id: 7832454551, name: 'usr', type: 'DIRECTORY' }, { id: 7832454554, name: 'applications', type: 'DIRECTORY', parentId: 7832454553 }, { id: 7832454555, name: 'mimeinfo.cache', type: 'FILE', parentId: 7832454554 }, { id: 7832454553, name: 'share', type: 'DIRECTORY', parentId: 7832454552 }, { id: 7832454552, name: 'local', type: 'DIRECTORY', parentId: 7832454551 } ] – Hajaj Khan Jun 21 '20 at 08:13
  • need to be all data in sequence – Hajaj Khan Jun 21 '20 at 08:16
  • it's working. it is not sorting the original data arrray, it returns the sorted output – newbie Jun 21 '20 at 08:18
  • u can see we need "7832454553" node on second number – Hajaj Khan Jun 21 '20 at 08:22