sort array based on another array

arr.sort((a, b) =>{  
	return sortingArray.indexOf(a) - sortingArray.indexOf(b);
});

3.78
9
Phoenix Logan 186120 points

                                    /**
 * 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;
    }
    
  });
  
  return array;
};


/**
 * Example:
 */

var item_array, item_order, ordered_array;

item_array = [ 
  { id: 2, label: 'Two' }
, { id: 3, label: 'Three' }
, { id: 5, label: 'Five' }
, { id: 4, label: 'Four' }
, { id: 1, label: 'One'}
];

item_order = [1,2,3,4,5];

ordered_array = mapOrder(item_array, item_order, 'id');

console.log('Ordered:', JSON.stringify(ordered_array));

3.78 (9 Votes)
0
3.83
6
Awgiedawgie 440220 points

                                    itemsArray.sort(function(a, b){  
  return sortingArr.indexOf(a) - sortingArr.indexOf(b);
});

3.83 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
sort array by order of another array javascript sort by other array sort array by other array js order array by another array js es6 sort an array likewise another array es 6 sort an array likewise another array sort an array based on another array javascript arrange array based on another array arrange one array based on another array in javascript js array sort firstby thenby sort array of arrays string by first element javascript sort array of arrays by first element javascript javascript sort by another array javascript order array like another array how to sort one array based on another javascript array get first sorted array how to sort an array with respect to another array es6 sort based on another array javascript sort array according to another array javascript sort array from other array javascript sort array like another array sort an array based on another array python sort an array based on another array js sort array from another array sort array based on another array argsort javascript reorder array by another array javascript sort true first order array based on another array how to sort one array by another array sort array by another arrays index javascript sort with other array how to sort an array with other array javascript sort an array by another array js order array by a value first then other sorting an array with respect to another array onlt get first element of array in sort js sort by another array sort the array base on another array sort an array using another array sort string array by another array value ordering js sort by first element in array js sort an array according to the other in js how to sort an array by value in another array sort one array based on another array how to sort array based on another array sort one array with respect to another how to sort an array based on another array in javascript JS sort a list of array by their first value javascript reorder array based on another array sort number first javascript arrray how to sort array based on values of another array sort an array with respect to another array sort array with respect to another array sort an array according to another array in js sort array as another array sort array by value in other arryt javascript sort to beginning of array array sort first item specific javascript how to sort one array with respect to another array can you sort by first item in array sort array based on another array nodejs js array sort by another array sort an array and put in another array sort an array by values based on another array sort by first to first charter js sort an array by a number from another array sort an array by another array javascript sort an array according to the order defined by another array javascript how to sort one array based on another javascript sort array with another array sort array on the basis of order in another array javascript array sort after first how to sort an array according to another array Sort an array according to the order defined by another array javascript sort first by then by then by how to sort one array according to another array sort array by 2 other arrays sort array according to two another array sort array according to another array order an array based on another array js Sort one array according to another array sort based on another array how to sort an array in accordance to another array sort an array with respect to another sort an array according to another array sort array of arrays based on first element sort array to match another array sort array based on another array javascript javascript sort items found to first javascript sort based on another array js sort according to another array sort array by another array js sort an js array on another array es6 sort array put item first javascript sort array with another array string javascript sort array with other array how to sort array in specific order js based on another array sort array by another javascript sort on array based on elements in another javascript sort elements in one array based on elements in another sort array based on another array parameter javascript sort array by another array id javascript sort array by another array javascript sort array against another array sort array following order of another array javascript javascript sort array by one element then another sort one array based on another sort an array in order of another array javascript sort array of objects based on another array javascript sort array of objects based on another array of objects javascript javascript order array by array sort an array based on another array js maintain order of an array from another array ec6 javascript maintain order of an array from another array javascript sort array and separate in others sort array by status and pass to first position js javascript sort array with another array sort array jquery and change ibdex to another sort one array based on another js sort one list by an other list js js sort array by another array how to find data in an array based on another array index using javascript how to sort an array based on another object in javascript sort array based in another array sort array in basis of another array javascript sort array with another array javascript javascript array sort reference to another how to get array in difrrent order rearrange an array using order of another array javascript sort one array based on another array javascript sort array of objects based on another array javascript when sort an array change the input array js reacrete array by object order sort array and move another array based on the array array sort is order another array sort with another given array in js js sort based on another array transfer sort order to another array sort array based another array javascript "How can you sort arrays of objects?: order array objects by another object order order list based on another list javascript how to sort strings javascript sort by string javascript js manual sort array javascript select which function for sorting arrange array javascript hwo tot sort throught an array from another function in javascript javascript reorder array of object by another array how to sort an array according to given order in js how to change one array order based on another in js sort an array depending on another array sort by ids of another array sort the same name in javascript js new array after sort how to sort an array according to another array typescript order array by another array key js js sort array on another array js sort by array sort array in js with function js sort array based on another array js order by other array js sort by other array javascript url sort syntax javascript order a list based on an other list order an array how to sort one array based on another array of objects in js sort arrays 1 according to array 2 javascript sort array javascript by element javscript .sort js sort by something then sort by something department subdepartment sorting in javascript javascript sort array on pararametr array sort for letters js javascript sortby sort function if one array is sorted the other should sort javacript how to sort list of array in js javascript array include order array sorting methods javascript reduce one array based on another javascript sort arrays like another array sort all the same value from the array in javascript sort js array of string js ordered arrya function to sort array in ascending order javascript javascript sort accensing sort array of object in js what is sorted array in javascript js sort array orden given by another array array js sort ascending create array based of another one js sort array with object javascript javascript sort elements by another array sort email array js jquery sort by another array sort array according to another array nodejs sort a list based on another list javascript reorder array by value javascript method to sort array in javascript array sorting based on another array value js sort barray of objects sort method in javascript dev docs sort array to new array finding a value in a sorted list js sort array from another array array sort examples visuliset array sort complex by string sorry array with another array js how does sort work sort array js sort object by title javascript sortBy array of objects how sort javascript works java script sort sorted in js sort array of arrays by other array javascript sort items displayed on a page useing .map to reorder an array js reorder array based on another array how to call an array that is constantly sorted javascript universal sort function javascript sort array example javascript sort by string javascript sort type sort one array based on another javascript sort array by another array javascript sort array based on some other value javascript arra.map sort by value order an array based on another array javascript order array by another array sort string by key string js strings sorter js return sorted string javascript sort by field ascending js sort object array in js js array sort feature how to sort an array based on another array sort a object of aaray in js sort array of objects get array before sort in js javascript arrays sorted alphabetize the array. order object array based on other array sort on array of object javascript es6 array reorder from another array es6 array reorder from other array typescript array reorder from other array typescript array reporder from other array Sort an array according to the order defined by another array typscript array reorder javascript array map sort objects javascript javascript. sort method js array sort not in place how to sort an array of strings in javascript js sort function in aray sort string js sort data comparing with different arrays javascript does sort return a new array compare sort string javascript implement sort in javascript javascript order of array sample js string sorted array js create sort funtion create js sort funtion sorting in js based om parameter javascript sort strings put array in sorted javascript javascript check sort values are same sort by values of anouther array sort an array on the basisi of another arraym js array sort objects order array by another array sort array based on another array js sort array based on another array sort array elements javascript how to sort and filter an array based on another array how to order an array based on another array javascript sort array of objects by number js sort array bt name javascript 6 sortby order array based on array elements js order array by another array sort javascript objects using Set sort javascript objects with Set sort by anothere array values sort array by another array arrange array of objects by another array of strings sort object array by another array sort object array by another object sort an object array in javascript array sort javascript by key with custom comparator compare order of other array sort array into new array sortin two d arrays depending on the value of other array javascript make an array match the order of another array typescript make an array match the order of another aray sort array of pairs javascript javascript sort name value js how to sort array ts order array based on another array js order array by javascript sort array based on another array sort method javascript how does array.sort work in javascript sort obj of integers js javascript iterate array of objects according to another array arrange array in ascending order javascript sort arry js es6 sort array of numbers array sort es6 javascript sort array to match another array sort array using another array compare to javascift sort sort if javascript how sort in js works array sort array.sort javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source