## sort array object using other array

not able to sort array object according to given array can you please help me out!!

reference link

const data = [ { parent_email: 'testuser@mailinator.com', childSurName: null, childFirstName: null, id: 22}, { parent_email: 'p20@mailinator.com', childSurName: null, childFirstName: 'Sachidanand', id: 31}, { parent_email: 'aa@aa.aaa', childSurName: null, childFirstName: null, id: 26}]; const order = [ 31,26 ]; const sorted = data.sort((a, b) => ( order.indexOf(a.id) - order.indexOf(b.id) )); console.log(sorted)

I need sorted result like my second array are and then after other data :

[ { parent_email: 'p20@mailinator.com', childSurName: null, childFirstName: 'Sachidanand', id: 31}, { parent_email: 'aa@aa.aaa', childSurName: null, childFirstName: null, id: 26} { parent_email: 'testuser@mailinator.com', childSurName: null, childFirstName: null, id: 22}, ];

but i am getting below result not getting proper sorted data:

[{ parent_email:"testuser@mailinator.com", childSurName:null, childFirstName:null, id:22 }, { parent_email:"p20@mailinator.com", childSurName:null, childFirstName:"Sachidanand", id:31 }, { parent_email:"aa@aa.aaa", childSurName:null, childFirstName:null, id:26 }]

You can use a `Map`

and `place values along with priority`

inside Map and then sort,

**Note:-** Also don't forget to keep the search value and key of same type

const data = [{parent_email: 'testuser@mailinator.com',childSurName: null,childFirstName: null,id: 22},{parent_email: 'p20@mailinator.com',childSurName: null,childFirstName: 'Sachidanand',id: 31},{parent_email: 'aa@aa.aaa',childSurName: null,childFirstName: null,id: 26}]; const order = ['31', '22']; const mapper = new Map(order.map((v, i) => [parseInt(v), i + 1])) const sorted = data.sort((a, b) => ( (mapper.get(a.id) || Infinity) - (mapper.get(b.id) || Infinity) )); console.log(sorted)

**Order an array of objects based on another array order · GitHub,** /** * Sort array of objects based on another array. */ function mapOrder (array, order, key) { array. sort( function (a, b) { var A = a[key], B = b[key]; if (order. indexOf(A) > order. indexOf(B)) { return 1; } else { return -1; } }); This method sorts a pair of one-dimensional array objects based on the keys in the first Array using the IComparable implementation of each key. Here, in the objects there are two array in which one contains the keys and the other contains the corresponding items. Syntax: public static void Sort (Array keys, Array items);

const sorted = data.sort((a, b) => order.indexOf(a.id.toString()) - order.indexOf(b.id.toString()) );

`id`

must be converted `toString`

.

Alternatively, change

const order = [ '31','22' ];

to

const order = [ 31,22 ];

**Sort an array according to the order defined by another array ,** Given two arrays A1[] and A2[], sort A1 in such a way that the relative order among the Method 1 (Using Sorting and Binary Search) Let size by another array. This is possible using an array method that returns a copy of the array. For example, Array.slice : const sortedBaz = baz.slice().sort(); // a new instance of the baz array is created and sorted

The problem is because of id `22`

. It does not exist in `order`

array and `indexOf`

function return `-1`

for this id. It means the `top priority`

, so it appears in the first place of result array.
You should handle ids that do not exist in the `order`

array.
For example (for sorting) you can do as below:

const sorted = data.sort((a, b) => { let aindex = order.indexOf(a.id); let bindex = order.indexOf(b.id); if(aindex === -1) return 1; if(bindex === -1) return -1; return aindex - bindex; });

**Which method will arrange the element of an array in alphabetical ,** How do you sort an array based on another array? Using sorted() on Python iterable objects. Python uses some extremely efficient algorithms for performing sorting. The sorted() method, for example, uses an algorithm called Timsort (which is a combination of Insertion Sort and Merge Sort) for performing highly optimized sorting.

**Array sorting based on another array? - JavaScript,** Which method will arrange the elements of an array in alphabetical order? You can use the sort() method of Array, which takes a callback function, which takes as parameters 2 objects contained in the array (which we call a and b): list . sort (( a , b ) => ( a . color > b . color ) ? 1 : - 1 )

**Sorting an array based on another array's order, quickly,** How do you sort an array of objects in Javascript? Sort (Array, Array, Int32, Int32) Sorts a range of elements in a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable implementation of each key.

**Quick Tip: How to Sort an Array of Objects in JavaScript,** Is anyone able to help to sort the dates in this format? var dates = [ 'May I realize that the lookup object is probably needed it this case So far I only know of a way to sort a simple one-dimensional array based on another sort() method is a java.util.Arrays class method. Syntax: public static void sort (int[] arr, int from_Index, int to_Index) arr - the array to be sorted from_Index - the index of the first element, inclusive, to be sorted to_Index - the index of the last element, exclusive, to be sorted This method doesn't return any value.