sort array based on another array js

Solutions on MaxInterview for sort array based on another array js by the best coders in the world

showing results for - "sort array based on another array js"
Lamont
25 Jan 2017
1arr.sort((a, b) =>{  
2	return sortingArray.indexOf(a) - sortingArray.indexOf(b);
3});
Simona
17 Feb 2017
1itemsArray.sort(function(a, b){  
2  return sortingArr.indexOf(a) - sortingArr.indexOf(b);
3});
Abdul
06 Oct 2020
1/**
2 * Sort array of objects based on another array
3 */
4
5function mapOrder (array, order, key) {
6  
7  array.sort( function (a, b) {
8    var A = a[key], B = b[key];
9    
10    if (order.indexOf(A) > order.indexOf(B)) {
11      return 1;
12    } else {
13      return -1;
14    }
15    
16  });
17  
18  return array;
19};
20
21
22/**
23 * Example:
24 */
25
26var item_array, item_order, ordered_array;
27
28item_array = [ 
29  { id: 2, label: 'Two' }
30, { id: 3, label: 'Three' }
31, { id: 5, label: 'Five' }
32, { id: 4, label: 'Four' }
33, { id: 1, label: 'One'}
34];
35
36item_order = [1,2,3,4,5];
37
38ordered_array = mapOrder(item_array, item_order, 'id');
39
40console.log('Ordered:', JSON.stringify(ordered_array));
queries leading to this page
javascript sort an array by another arraysort an array on the basisi of another arraym jsorder array by another array jssort one array based on another array javascripthow to sort an array of strings in javascriptif one array is sorted the other should sort javacriptjavascript order array like another arrayjavascript order of arraysort array by another arrayarray reorder javascriptjavascript sort array by another array idimplement sort in javascriptorder an arraysort an array with respect to another arraysort number first javascript arrraysort array of objects based on another array javascriptsort an array based on another arraysort array by status and pass to first position jsjavascript iterate array of objects according to another arrayhow to sort an array with respect to another arraysort array by value in other arrytjavascript sort array against another arrayhow to sort array based on another arrayjs how to sort arrayjavascript universal sort functionsort values based on another arraysort arraysort string by key string jssort array based on other arraysort array based on another array parameterorder an array based on another array jssort array on the basis of order in another arraycreate array based of another one jsjavascript sort one array based on anothersort with another given array in jsjavascript sort array from another arraysort by ids of another arrayorder object array based on other arraysort all the same value from the array in javascriptjs how does sort worksort by first element in array jsjavascript order array according to another arrayhow to order an array based on another arraysort array by anothersort an array by values based on another arrayalphabetize the array javascript sort typemake an array match the order of another aray sort array and move another array based on the arrayhow to sort an array in accordance to another arraysort if javascriptreacrete array by object orderhow to get array in difrrent orderarray sort objectsarrange one array based on another array in javascriptorder array based on array elementsjavascript sort by stringsort one array based on another jsorder list based on another list javascriptjs sort array after adjust value firstsort array according to another array nodejssort one array based on anotherjavascript sort array based on another arrayjavascript 6 sortbysort array from other arrayjs sort array based on another arrayhow to sort an array based on another arrayhwo tot sort throught an array from another function in javascripthow to change one array order based on another in jsjquery sort by another arraysort array by another arrays indexsort object array by another objectjs sort function in araysort an array with respect to anotherdoes sort return a new arraysort the same name in javascriptsort array by order of another arraystrings sorter jsarray sorting based on another array valuehow to sort one array based on anotherhow to sort array based on values of another arrayarrange array javascriptsort array of objects based on another array of objects javascriptarrange array based on another arrayjavascript sort string array based on another array in javascript js array sort firstby thenbysort array as another arraysorting in js based om parameterrearrange an array using order of another array javascriptsort an array using another arrayjavascript array sort reference to anotherarrange array of objects by another array of stringsjs array sort featurejavascript sort array by one element then anotherjavascript arra map sort by valuejavascript sort items found to firstjavascript sort array with another array stringsort an array based on another array jssort array javascript by elementhow to sort an array according to another array conditionarray js sort ascending onlt get first element of array in sorthow to find data in an array based on another array index using javascriptjavascript reduce one array based on anothersort array following order of another array javascriptjavascript order a list based on an other listjavascript sort items displayed on a pagesample js string sorted arrayjavascript reorder array based on another arraysort by heighes nr first javascripthow to sort and filter an array based on another arraysort array using another arraysort array according to another arrayjavascript sort true firstsort data comparing with different arrays javascriptarray sort javascript by key with custom comparatorjavascript sort first by then by then bycan you sort by first item in arraysort array of object in jsjavascript sort array like another arraysort array based another array javascripthow to sort an array according to another array typescriptarray sort examples visulisetes6 sort array put item firstsort array by another array pythonsort object array by another arraysort the first value in javascriptsort array missed first valefunction to sort array in ascending order javascriptjs sort array orden given by another arraywhen sort an array change the input array jsenter the first array and second array then sortarray sort first item specific javascriptsort javascript objects with setjs sort based on another arrayjavascript sort by another arraysort array from another arrayjavascript array get first sorted arrayjavascript sort arrays like another arraysort array of objectssort array based on another array javascriptjavascript sort accensingget array before sort in jssort by another arraysort array of arrays by other arrayjavascript sort array of objects by numberjavascript sort array on pararametrjavascript sort array examplesort array based on some other valuejs sort by other arrayjs order array by another arrayjavascript array include orderjavascript sort array by another arrayjavascript array sort by another arraycompare to javascift sortjavascript sort array from other arrayjs sort by arrayjavascript sortbymake an array match the order of another array typescriptsort an array according to another array in jssorting an array with respect to another arraysort an array according to the other in jssort one list by an other list jspython sort array based on another arrayjs order by other arrayjavascript sort elements in one array based on elements in anotherhow does array sort work in javascriptorder array based on another arraysort array of arrays based on first elementsort array and separate in otherssort array based on another array with numbersjs sort array by another arraysort email array jsjavascript sort based on another arrayjs array sort by another arraysort array to new arrayhow to sort an array based on another object in javascriptjs sort barray of objectshow to sort one array with respect to another arraysort the array according to another arrayes6 array reorder from other arrayjavscript sortsort one array with respect to anothermaintain order of an array from another array ec6 javascriptsort method in javascript dev docsjs manual sort arrayjs sort by something then sort by somethingsort javascript objects using setsort arry jssort string array by another array value ordering jssort array of arrays by first element javascripthow to sort an array according to given order in jshow to sort an array based on the value of another array javascriptarray map sort objects javascriptsort array by other array jsjavascript sort elements by another arrayjavascript sort stringsmethod to sort array in javascriptorder array objects by another object orderjavascript reorder array of object by another arraysort by lowest nr first javascriptjavascript sort array to match another arrayarray sortsort on array of object javascriptsort array with respect to another arraysort array into new arraysort array in basis of another array javascriptjavascript check sort values are samejavascript arrays sortedjavascript select which function for sortingjavascript sort to beginning of arraysort an array depending on another arrayuseing map to reorder an array jssort an array according to the order defined by another array javascriptjs sort according to another arraysort by values of anouther arraycompare sort string javascriptsort based on another arraysort highest first javascriptsorry array with another arrayes6 array reorder from another arraysort an object array in javascriptsort one array according to another arraysort array with another arrayjs sort by another arrayjs sort array bt namejavascript array sort after firstjavascript url sort syntaxsort one array based on another javascriptreorder array by value javascriptjs create sort funtionsort an array according to another arraysort an array and put in another arrayjava script sortarray sort javascriptjs array sort not in placejavascript sort methodsort array based on another array pythonsort array in js with functionwhat is sorted array in javascriptdepartment subdepartment sorting in javascriptsort an array based on another array pythonarray sort complex by stringhow to sort one array by another arrayjs order array bysort by field ascending jstransfer sort order to another arraycompare order of other arraysort array with object javascriptjavascript sort on array based on elements in anotherhow to sort an array by value in another arrayes6 sort array of numbersjavascript sort array with other arraysort by first to first charter jsjs sort object by titlesort array based in another array javascript sort by other arrayhow to sort one array based on another javascriptarray sort is order another arrayhow to sort an array based on another array in javascriptput array in sorted javascriptjavascript reorder array by another arraysorted in jses6 sort an array likewise another arraysort array by another array jstypescript array reorder from other arraysort array based on another array jsjs sort array on another arrayjavascript sort with other arraysort a list based on another list javascriptsort method javascripthow to sort one array according to another arraysort array by another array javascriptsortin two d arrays depending on the value of other array javascriptarray sort es6how to sort array in specific order js based on another arraysort array according to two another array sort arrays 1 according to array 2 javascriptsort arrays according to first array jsreturn sorted string javascripthow to sort strings javascriptsort an array according to another array typescriptorder array by another arrayes6 sort based on another arrayjs sort array as anotherhow to sort an array with other arraysort obj of integers jsreorder array based on another arrayhow to sort list of array in jssort an array in order of another array javascriptsort array of objects based on another array javascripthow to sort an array according to another array javascriptsort a object of aaray in jsjavascript sort name valuesort an array by another array javascriptsort array jquery and change ibdex to anotherjs new array after sortjs sort a list of array by their first valuetypescript array reporder from other arrayhow to sort an array according to another arraysort an array based on another array javascriptorder array by another array key jssort array based on another arraysort string jssort array of arrays string by first element javascriptarray sorting methodssort one array based on another arrayjs order array by a value first then otherarray sort for letters jsmaintain order of an array from another array javascriptsort by string javascriptsort by anothere array valuesjavascript sortby array of objects 22how can you sort arrays of objects 3f 3asort object array in jssort array elements javascriptes 6 sort an array likewise another arrayhow to call an array that is constantly sortedsort js array of stringsort array of pairs javascriptsort one array based on another array pythonjavascript sort array according to another arraysort an array by a number from another arrayarrange array in ascending order javascriptsort an array according to the order defined by another arrayjs ordered arryajavascript sort array with another arraysort functionsort array to match another arraysort an array according to the order defined by another array typscriptjavascript order array by another arrayts order array based on another arraysort array based on another array argsortfinding a value in a sorted list jssort the array base on another arraycreate js sort funtionsort array by 2 other arrayshow sort javascript workshow to sort one array based on another array of objects in jsorder array based on another array with ordersort an js array on another arraysort array based on another array nodejsjs sort array from another arrayhow sort in js worksjavascript order array by arraysort array with another array javascriptorder an array based on another arraysort array based on another array js