How do I sort an array of objects based on another array of objects

sort an array based on another array python
sort an array of object based on another array
javascript sort array of objects by multiple properties
javascript sort array of objects alphabetically
sort nested array of objects javascript
javascript sort array of objects by key
typescript sort array of objects
angular 6 sort array of objects by property

I have an array of a dynamic sizing that changes its order based on what is last selected to position 0 of the array. Problem is I want to keep the original order of the array.

In order to do that, I have created an additional variable that is set to the original sorting of the array. Although some of the values of the array might change after the selecting of the item, the name property never does. I want to use this fact to sort the new array to the original position.

let keepRateOrder = rates.sort((a, b) => {  
        return prevRates.indexOf(a.name) - prevRates.indexOf(b.name);
      });


const rates = [
{name:'UPS', isChoosen:true, cost:63 ...},
{name:'Mail', isChoosen:false, cost:23 ...},
{name:'FedEx', isChoosen:false, cost:33 ...}
]


const prevRates = [
{name:'Mail', isChoosen:false, cost:23 ...},
{name:'UPS', isChoosen:true, cost:63 ...},
{name:'FedEx', isChoosen:false, cost:33 ...}
]

This can be solved using findIndex. In the following example the sort will put Mail first as per the prevRates array:

const rates = [
  {name:'UPS', isChoosen:true, cost:63},
  {name:'Mail', isChoosen:false, cost:23},
  {name:'FedEx', isChoosen:false, cost:33}
];

const prevRates = [
  {name: 'Mail'},
  {name: 'UPS'},
  {name: 'FedEx'},
];

let keepRateOrder = rates.sort((a, b) => {
  return prevRates.findIndex(p => p.name === a.name) - prevRates.findIndex(p => p.name === b.name);
});

console.log(keepRateOrder);

Sort an array of objects based on another array of ids, sort(function (x, y) { return c[x.id] - c[y.id]; }); document. write('<pre>' + JSON. stringify(b, 0, 4) + '</pre>'); For greater objects, I suggest to use Sorting with map. Array.sort in a nutshell Array.sort takes an array and — you guessed it — sorts it in place. No copy of the array is created (as with map, etc.), so the array itself is altered with the sorted

First use .map to transform the prevRates into an array of just the name properties, and then you can use .sort based on that array of names:

const prevRates = [
  {name:'Mail', isChoosen:false, cost:23 },
  {name:'UPS', isChoosen:true, cost:63 },
  {name:'FedEx', isChoosen:false, cost:33 }
];
const rates = [
  {name:'UPS', isChoosen:true, cost:63 },
  {name:'Mail', isChoosen:false, cost:23 },
  {name:'FedEx', isChoosen:false, cost:33 }
];

const prevRatesNames = prevRates.map(({ name }) => name);
rates.sort((a, b) => (
  prevRatesNames.indexOf(a.name) - prevRatesNames.indexOf(b.name)
));
console.log(rates);

Quick Tip: How to Sort an Array of Objects in JavaScript, Basic Array Sorting. By default, the JavaScript Array.sort function converts each element in the array that needs to be sorted into a string, and  * 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 ] ;

How about adding a new property to each item to track the original index, originalIndex. Sort using that when you want the original order back, avoids the extra copy. Assuming that's what you want to do!

Array sorting based on another array? - JavaScript, I realize that the lookup object is probably needed it this case I only know of a way to sort a simple one-dimensional array based on another  Recall that by default, to sort an array containing primitive values (such as strings) alphabetically, you would simply call the sort() method without any comparison function passed in directly on the array, such as myarray.sort(). This doesn't work however when the data we wish to sort using is deep inside a property of the array object, and not the array itself.

JavaScript Array sort: Sorting an Array More Effectively, This tutorial shows you how to use the JavaScript array sort method to sort arrays of numbers, arrays of strings, and arrays of objects by a property. The sort() method will sort elements based on the return value of the compare() function with  Use Arrays.sort () method to sort an array of objects. The objects must implement the Comparable interface which will decide their sorting order. All elements in the array must implement the Comparable interface. All elements in the array must be mutually comparable and must not throw a ClassCastException while comparing.

Sorting an array based on another array's order, quickly, I'm going to assume that in the general case these ids might be strings and might be in a non-natural order in both first and second , since  Using Array.sort alone wouldn’t be very useful for sorting an array of objects. Thankfully, the function takes an optional compareFunction parameter, which causes the array elements to be sorted

How to sort an array of objects in JavaScript, Instead a custom function, that can be passed to the built-in sort() method to sort objects based on the specified property, is needed for comparison. svg viewer  When we return 1, the function communicates to sort() that the object b takes precedence in sorting over the object a.Returning -1 would do the opposite.. The callback function could calculate other properties too, to handle the case where the color is the same, and order by a secondary property as well:

Comments
  • @p.s.w.g keepRatesOrder is incorrect, my problem is just sorting rates to the same order prevRates was in. prevRates is just a copy of the rates array before it was changed. I would just like to return it to its original order.
  • @user7659932 Which one is the unique key? The name?
  • @NikKyriakides yes, the rest of values are subject to change
  • @CertainPerformance there is an example of what prevRates might look like, the only difference between the two is the order and some of the other properties not listed might be different.
  • Why not have a variable point to the selected rate? Somewhere isChoosen is being set for each one every time there's an update, instead it could just update a variable, or set it to null if nothing is selected.