
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Sorting by 'next' and 'previous' properties (JS comparator function)
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' } ]
- Related Articles
- Sorting JavaScript object by length of array properties.
- Use previous and next class for alignment of links
- How to create "next" and "previous" buttons with CSS?
- Java Program to get previous and next index using ListIterator
- Comparator function of qsort() in C
- Elements greater than the previous and next element in an Array in C++
- How to create previous and next button and non-working on end position using JavaScript?
- Autocorrelation Function and its Properties
- How to find the previous and next record using a single query in MySQL?
- Golang Program to round up the next previous power of 2.
- Obtain the Previous Index and Next Index in an ArrayList using the ListIterator in Java
- Cross Correlation Function and its Properties
- How to create the previous and next buttons and non-working on the end positions using JavaScript?
- next() function in PHP
- Using next() function in Express.js
