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

When working with linked data structures in JavaScript, you may encounter objects that reference each other through 'next' and 'previous' properties. This tutorial demonstrates how to sort such objects into their correct sequential order.

Problem Statement

Consider an array of objects representing pages of a website, where each object has:

  • An id property for unique identification
  • A next property pointing to the next page's id (unless it's the last page)
  • A previous property pointing to the previous page's id (unless it's the first page)

These objects are randomly positioned in the array, and we need to sort them into their correct sequence.

Sample Data

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" },
];

Two-Step Solution

Step 1: Create Map and Find First Object

We create a Map for quick lookup and identify the first object (one without a 'previous' property):

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: Traverse the Chain

Starting from the first object, we follow the 'next' references to build the sorted array:

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

console.log(sortedArray);

Complete 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);
[
  { 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' }
]

How It Works

The algorithm efficiently sorts linked objects by:

  1. Creating a Map for O(1) lookup of objects by their id
  2. Finding the starting point (object with no 'previous' property)
  3. Following the chain of 'next' references until reaching the end

This approach has O(n) time complexity and works for any properly linked chain of objects.

Conclusion

This solution demonstrates how to reconstruct the correct order of linked objects using Map-based lookup and chain traversal. The technique is useful for sorting pagination data, linked lists, or any sequential object structure.

Updated on: 2026-03-15T23:18:59+05:30

431 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements