showing results for - "sort array based on another array"
Elisa
27 Feb 2017
1arr.sort((a, b) =>{  
2	return sortingArray.indexOf(a) - sortingArray.indexOf(b);
3});
Davide
04 May 2019
1itemsArray.sort(function(a, b){  
2  return sortingArr.indexOf(a) - sortingArr.indexOf(b);
3});
Roberto
08 Sep 2019
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
sort array based in another array sort array by order of another arraysort array by value in other arrytsort javascript objects with setsort method javascriptsort array of object in jssort array based on another array jsjs sort by another arrayhow to sort an array with respect to another arraysort an array according to another array in jsjavascript order a list based on an other listarray sort is order another arraysort an array according to the order defined by another array typscriptsort array based on another arrayjavascript sort array by first elementjavascript reorder array of object by another arraysort array by other array jssort on array of object javascriptsort a list based on another list javascriptjs sort object by titlemethod to sort array in javascriptsort array based on another array with numbersjs sort array to match another arrayjs sort array by another arrayjs sort by something then sort by somethingsort array on the basis of order in another arraysort an array by a number from another arrayjavascript sort array by another arraysort an array based on first item in javascripthow sort javascript worksjavascript use array to set order of another arraysort an array based on another array javascriptsort obj of integers jssort array based on another array pythonhow to sort one array based on another javascriptsort subarrays by first elemtent javascriptorder an arraysample js string sorted arraysort one array based on another algorithmjs sort a list of array by their first valueimplement sort in javascriptsort string jsjava sort array based on another arrayarrange array based on another arrayorder array by another arraymaintain order of an array from another array ec6 javascriptsort an array with respect to anotherreacrete array by object orderjs sort array from another arrayjavascript sort stringshow to get array in difrrent orderjavascript sort array with other arraysort array by anothersort one array based on another jssort array following order of another array javascriptjavascript sort an array by another arrayhow to sort list of array in jsjavascript order array by arrayarray sort complex by stringtypescript array reporder from other arrayjavascript sort array of arrays by first elementsort an array depending on another arrayreorder array by value javascriptorder an array based on another arrayorder array by another array key jsfunction to sort array in ascending order javascriptsort object array by another arrayjavascript sort array to match another arraysort array of arrays by first element javascriptsort array by another array javascriptarrange array of objects by another array of stringssort an array based on another arrayhow to sort one array by another arrayarray sort first item specific javascriptsort array javascript by elementjavascript sort arrays like another arraysort one array according to another arrayjavascript sort on array based on elements in anotherhow to find data in an array based on another array index using javascriptsort array matching another arrayjs create sort funtionhow to sort an array in accordance to another arrayjavascript sort elements by another arrayjs order array bysort an object array in javascriptjavascript sort array according to another arrayhow sort in js worksjavascript sort array of objects by numbersort the first value in javascriptsort by lowest nr first javascriptjavascript order array by another arraysort an array based on first itemjavascript sort typejs sort array as anothersort array into new arrayfinding a value in a sorted list jssort arry jssort string array by another array value ordering jsjavascript sort array from other arraysort an array based on another array pythonjs sort based on another arrayjavascript sort array examplejs array sort not in placehow to sort an array by value in another arrayfirst order function in javascriptjavascript sort elements in one array based on elements in anotherjs sort array orden given by another arraysort array according to two another array sort by first element in array jssort array to new arraysort array of objects based on another array of objects javascriptsort an array using another arrayjavascript sort to beginning of arrayjs sort by arrayjavascript universal sort functionjavascript order list by another arrayjs sort barray of objectsjavascript sort array by one element then anothersort an array according to the order defined by another arraytypescript array reorder from other arraysort array by another array jshow to sort an array based on the value of another array javascriptsort array by status and pass to first position jsjavascript order of arrayjavascript sort array like another arrayhow does array sort work in javascriptsort arrayhow to sort one array based on anothersort an array according to another arrayjavascript sort array from another arraysort by string javascriptsort by another arrayes6 sort an array likewise another arraysort string by key string jssort by anothere array valuesjavascript 6 sortbysort number first javascript arrrayjs new array after sortsort by first letter javascriptsort based on another arraysort with another given array in jsjavascript select which function for sortingarray sorting methodsjs how to sort arraysort array jquery and change ibdex to anotherreorder array based on another arraymake an array match the order of another aray compare to javascift sortsort an array based on another array jsarray sorting based on another array valuereturn sorted string javascriptjavascript sort array of arrays by first valuejavascript sort methodsort object array by another objectsorry array with another arraysort an array with respect to another arrayes6 array reorder from another arraysort one array based on another arrayorder array based on another arrayhow to sort array based on values of another arraysort one array based on anothersort by field ascending jsjavascript array sort by another arrayjavscript sorthow to sort an array based on another arrayjavascript sort with other arraysort array with respect to another arrayalphabetize the array es6 sort array of numbersarray sortjavascript iterate array of objects according to another arrayjs sort array based on another arrayenter the first array and second array then sortarrange array in ascending order javascriptsort method in javascript dev docses6 sort based on another arrayjs sort according to another arraysort base on first item in jsjavascript sort array by another array idsort the same name in javascripthow to sort array in specific order js based on another arrayjs sort by other arrayjs array sort by another arraysort js by first letteerget array before sort in jssort array of pairs javascriptts order array based on another arraysort array with another array javascripthow to sort one array with respect to another arrayjs how does sort worksort one array based on another array pythonjavascript sort array with another arrayjavascript sort array with another array stringdepartment subdepartment sorting in javascriptmaintain order of an array from another array javascripthwo tot sort throught an array from another function in javascriptjs sort items from one array to anotherarray reorder javascriptjavascript reorder array based on another arraysort javascript objects using setsort array based on another array javascriptsort an array by values based on another arraysort array in basis of another array javascriptsort js array of stringorder object array based on other arrayjs sort function in araysort object array in jses6 array reorder from other arraysort array according to another arraysort array based on another array numpyhow to sort an array javascript based on another arraysort array missed first valesort array of arrays by other arraymake an array match the order of another array typescriptjavascript sort array against another arraysort an array according to another array typescripthow to sort an array according to another array typescriptsort array in js with functionjavascript array get first sorted arraysort one array with respect to anotherhow to sort one array based on another array of objects in jshow to sort an array according to given order in jssort an array and put in another arrayarray sort javascriptjs order array by a value first then othersort array elements by first element javascripthow to sort the first element to an array javascriptjs sort array as another arrayjavascript order array like another arrayarray sort objectsonlt get first element of array in sortjavascript sortby array of objectsjavascript sort items displayed on a pagesort array with another arraycreate js sort funtionjavascript sort array by started by first elementsort all the same value from the array in javascriptsort an js array on another arraysorted in jsorder array based on array elementsarrange one array based on another array in javascriptjs array sort firstby thenbyhow to sort array based on another arrayjavascript array sort after firsthow to change one array order based on another in js sort older first jsjavascript sort array based on another arrayhow to sort and filter an array based on another arrayjavascript sort string array based on another array in javascript sorting an array with respect to another arraysort array from other arraysort array based on other arraycan you sort by first item in arrayhow to sort an array based on another array in javascriptsort numpy array based on another arraysort a array depend on another arraysort array with object javascriptsort by ids of another arrayarray sort examples visulisetjavascript sort by stringjavascript sort by another arraycreate array based of another one jssort array and separate in othershow to call an array that is constantly sortedarrange array javascriptes6 sort array put item firsthow to sort the first value in an array javascripthow to sort one array according to another arraysort highest first javascriptjs sort array on another arrayorder list based on another list javascriptjavascript arra map sort by valuehow to sort an array based on another object in javascriptsort array and move another array based on the arrayjs sort array bt namearray sort javascript by key with custom comparatorsort one list by an other list jsarray sort es6sort a object of aaray in jsjavascript sort based on another arrayjavascript reduce one array based on anotherput array in sorted javascriptsort one array based on another array javascriptjavascript sort name valuesort email array jsjavascript sort by other arraysort array based on first item in javascriptthow to sort an array according to another arraysorting in js based om parameterwhen sort an array change the input array jssort by heighes nr first javascriptjs manual sort arrayorder array based on another array with ordersort data comparing with different arrays javascriptsort values based on another arrayarray js sort ascending javascript check sort values are samesort array based on another array argsortjavascript url sort syntaxsort array from another arraysort array by another array pythonjavascript arrays sortedarray sort by another array indexjavascript sortbyjs ordered arryawhat is sorted array in javascriptcompare order of other arraysort by first to first charter jssort one array based on another javascriptsort array of objects based on another array javascripthow to sort an array with other arraysort array of arrays based on first elementhow to sort strings javascripthow to sort second array like first array javascriptjava script sortsort array elements by first element in array javascripthow to sort an array according to another array javascriptsort array based on some other valuecompare sort string javascriptsort an array in order of another array javascriptsort array elements javascriptsort an array by another array javascriptsort if javascriptjavascript sort array except first elementjs array sort first element to the endif one array is sorted the other should sort javacriptrearrange an array using order of another array javascriptjavascript sort true firstjs array sort featurejavascript sort first by then by then byarray sort for letters jsorder an array based on another array jsjavascript array sort reference to anotherjavascript sort array on pararametrorder array by another array jssort an array according to the other in jsuseing map to reorder an array jsjs order array by another arraysort array by another array 22how can you sort arrays of objects 3f 3asort an array based on another array javasort the array according to another arraysort array based on another array parametersort arrays 1 according to array 2 javascriptsort the array base on another arraysort array by 2 other arraysjs sort one array by anotherjavascript sort items found to firstsortin two d arrays depending on the value of other array javascriptjavascript sort accensingjavascript sort one array based on anotheres 6 sort an array likewise another arraysort array of objectsjs sort array after adjust value firstsort array according to another array nodejstransfer sort order to another arrayhow to sort an array according to another array conditionsort array to match another arrayjavascript reorder array by another arraysort array of arrays string by first element javascriptstrings sorter jssort array by another arrays indexsort array based another array javascriptsort array of objects based on another array javascriptsort array using another arrayjquery sort by another arraysort array based on another array nodejshow to sort an array of strings in javascriptjavascript array include orderjs order by other arrayhow to order an array based on another arrayarray map sort objects javascriptsort arrays according to first array jsjavascript order array according to another arraypython sort array based on another arraysort functionsort by values of anouther arraysort array as another arraysort an array on the basisi of another arraym jssort an array according to the order defined by another array javascriptorder array objects by another object orderdoes sort return a new arraysort array based on another array