Sorting by 'next' and 'previous' properties (JS comparator function)

JavascriptWeb DevelopmentObject Oriented Programming

Here is our sample array of object, consider each object as representing some page of a multipage website, each object have a next property (unless it represents the last page) that points to some id of another object and a previous property (unless it represents the first page) that points to some id of its previous object.

These objects are all positioned randomly right now, our job is to sort them to their correct position −

let arr = [
   { id: "1325asdfasdasd", next: "5345341fgdfgdd", previous:"545234123fsdfd" },
   { id: "das987as9dya8s", next: "3j12k3b1231jkj" },
   { id: "89ad8sasds9d8s", previous: "1j3b12k3jbasdd" },
   { id: "5345341fgdfgdd", next: "1j3b12k3jbasdd", previous:"1325asdfasdasd" },
   { id: "1423123123asfd", next: "545234123fsdfd", previous:"3j12k3b1231jkj" },
   { id: "1j3b12k3jbasdd", next: "89ad8sasds9d8s", previous:"5345341fgdfgdd" },
   { id: "3j12k3b1231jkj", next: "1423123123asfd", previous:"das987as9dya8s" },
   { id: "545234123fsdfd", next: "1325asdfasdasd", previous:"1423123123asfd" },
];

We need to sort it so that the objects with no previous comes first and one with no next comes last and with next and previous pointing to correct ids

We will tackle this problem in two steps −

Step 1 − We iterate over the whole array, store the id as key and object as value in a map and store the object with no previous in a separate variable −

const objectMap = new Map();
let firstObject;
const sortedArray = [];
for(const obj of arr){
   objectMap.set(obj.id, obj);
   if(!obj.previous){
      firstObject = obj;
   }
}

Step 2 − We start a loop over the elements of map and start accessing and pushing the next objects of each member while we reach the end −

for(let start = firstObject; start; start = objectMap.get(start.next)){
   sortedArray.push(start);
};
console.log(sortedArray);

Let us now see the complete example with output −

Example

let arr = [
   { id: "1325asdfasdasd", next: "5345341fgdfgdd", previous:"545234123fsdfd" },
   { id: "das987as9dya8s", next: "3j12k3b1231jkj" },
   { id: "89ad8sasds9d8s", previous: "1j3b12k3jbasdd" },
   { id: "5345341fgdfgdd", next: "1j3b12k3jbasdd", previous:"1325asdfasdasd" },
   { id: "1423123123asfd", next: "545234123fsdfd", previous:"3j12k3b1231jkj" },
   { id: "1j3b12k3jbasdd", next: "89ad8sasds9d8s", previous:"5345341fgdfgdd" },
   { id: "3j12k3b1231jkj", next: "1423123123asfd", previous:"das987as9dya8s" },
   { id: "545234123fsdfd", next: "1325asdfasdasd", previous:"1423123123asfd" },
];
const objectMap = new Map();
let firstObject;
const sortedArray = [];
for(const obj of arr){
   objectMap.set(obj.id, obj);
   if(!obj.previous){
      firstObject = obj;
   }
}
for(let start = firstObject; start; start = objectMap.get(start.next)){
   sortedArray.push(start);
};
console.log(sortedArray);

Output

The output of the code in the console will be −

[
   { id: 'das987as9dya8s', next: '3j12k3b1231jkj' },
   {
      id: '3j12k3b1231jkj',
      next: '1423123123asfd',
      previous: 'das987as9dya8s'
   },
{
   id: '1423123123asfd',
   next: '545234123fsdfd',
   previous: '3j12k3b1231jkj'
},
{
   id: '545234123fsdfd',
   next: '1325asdfasdasd',
   previous: '1423123123asfd'
},
{
   id: '1325asdfasdasd',
   next: '5345341fgdfgdd',
   previous: '545234123fsdfd'
},
{
   id: '5345341fgdfgdd',
   next: '1j3b12k3jbasdd',
   previous: '1325asdfasdasd'
},
{
   id: '1j3b12k3jbasdd',
   next: '89ad8sasds9d8s',
   previous: '5345341fgdfgdd'
},
{    id: '89ad8sasds9d8s', previous: '1j3b12k3jbasdd' }
]
raja
Published on 18-Aug-2020 10:30:16
Advertisements