
- 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
Sort by index of an array in JavaScript
Suppose we have the following array of objects −
const arr = [ { 'name' : 'd', 'index' : 3 }, { 'name' : 'c', 'index' : 2 }, { 'name' : 'a', 'index' : 0 }, { 'name' : 'b', 'index' : 1 } ];
We are required to write a JavaScript function that takes in one such array.
The function should sort this array in increasing order according to the index property of objects.
Then the function should map the sorted array to an array of strings where each string is the corresponding name property value of the object.
Therefore, for the above array, the final output should look like −
const output = ["a", "b", "c", "d"];
Example
The code for this will be −
const arr = [ { 'name' : 'd', 'index' : 3 }, { 'name' : 'c', 'index' : 2 }, { 'name' : 'a', 'index' : 0 }, { 'name' : 'b', 'index' : 1 } ]; const sortAndMap = (arr = []) => { const copy = arr.slice(); const sorter = (a, b) => { return a['index'] - b['index']; }; copy.sort(sorter); const res = copy.map(({name, index}) => { return name; }); return res; }; console.log(sortAndMap(arr));
Output
And the output in the console will be −
[ 'a', 'b', 'c', 'd' ]
- Related Articles
- Sort an array of objects by multiple properties in JavaScript
- Sort Array of objects by two properties in JavaScript
- Sort array by month-year JavaScript
- Finding the index position of an array inside an array JavaScript
- Odd even sort in an array - JavaScript
- Sort an array according to another array in JavaScript
- Remove values in an array by comparing the items 0th index in JavaScript?
- Sort array of objects by string property value in JavaScript
- Using merge sort to recursive sort an array JavaScript
- Sort array of objects by string property value - JavaScript
- Sort array by year and month JavaScript
- Sorting an array of literals using quick sort in JavaScript
- How to sort an array of integers correctly in JavaScript?
- JavaScript Bubble sort for objects in an array
- C program to sort an array by using merge sort

Advertisements