
- 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
Use array as sort order in JavaScript
const sort = ["this","is","my","custom","order"]; const myObjects = [ {"id":1,"content":"is"}, {"id":2,"content":"my"}, {"id":3,"content":"this"}, {"id":4,"content":"custom"}, {"id":5,"content":"order"} ];
We are required to write a JavaScript function that takes in two such arrays and sorts the second array of objects on the basis of the first array so that the content property of objects are matched with the strings of the first array.
Therefore, for the above arrays the output should look like −
const output = [ {"id":3,"content":"this"}, {"id":1,"content":"is"}, {"id":2,"content":"my"}, {"id":4,"content":"custom"}, {"id":5,"content":"order"} ];
Example
The code for this will be −
const arrLiteral = ["this","is","my","custom","order"]; const arrObj = [ {"id":1,"content":"is"}, {"id":2,"content":"my"}, {"id":3,"content":"this"}, {"id":4,"content":"custom"}, {"id":5,"content":"order"} ]; const sortByReference = (arrLiteral, arrObj) => { const sorted = arrLiteral.map(el => { for(let i = 0; i < arrObj.length; ++i){ if(arrObj[i].content === el){ return arrObj[i]; } }; }); return sorted; }; console.log(sortByReference(arrLiteral, arrObj));
Output
And the output in the console will be −
[ { id: 3, content: 'this' }, { id: 1, content: 'is' }, { id: 2, content: 'my' }, { id: 4, content: 'custom' }, { id: 5, content: 'order' } ]
- Related Articles
- Implementing insertion sort to sort array of numbers in increasing order using JavaScript
- Which algorithm does the JavaScript Array#sort() function use?
- Finding sort order of string in JavaScript
- Decreasing order sort of alphabets in JavaScript
- Sort an array in descending order using C#
- JavaScript Program to Count rotations required to sort given array in non-increasing order
- Golang Program To Sort An Array In Ascending Order Using Insertion Sort
- Golang Program to sort an array in descending order using insertion sort
- How to Sort/Order keys in JavaScript objects ?
- How to use map() on an array in reverse order with JavaScript?
- Java Program to sort an array in alphabetical order
- How to sort Java array elements in ascending order?
- C# program to sort an array in descending order
- C program to sort an array in descending order
- Sort an array and place a particular element as default value in JavaScript

Advertisements