Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript sort sparse array keep indexes

What is the best method to sort a sparse array and keep the elements on the same indexes? For example:

a[0] = 3,  a[1] = 2,  a[2] = 6, a[7] = 4, a[8] = 5, 

I would like after the sort to have

a[0] = 2,  a[1] = 3,  a[2] = 4,  a[7] = 5,  a[8] = 6. 
like image 511
TestersGonnaTest Avatar asked Aug 27 '12 07:08

TestersGonnaTest


People also ask

Can you sort array while maintaining index?

Solution to Preserve indices while sorting arrayIn this Comparator, we preserve the actual array which is to be sorted. Also, if you notice, the compare method compares the values of the actual array based on the indices supplied to it. The key here is to supply the indices and not the actual values to be sorted.

Does sort mutate array JavaScript?

The sort() method returns a reference to the original array, so mutating the returned array will mutate the original array as well.

How do you sort an index array?

Approach: Create an array of indices and store 0 to n-1 (n is the size of the given array). Now sort the array by overriding the Comparator in which you will compare the elements of the given array for indices array.

How do you sort an array in ascending order?

Example: Sort an Array in Java in Ascending Order Then, you should use the Arrays. sort() method to sort it. That's how you can sort an array in Java in ascending order using the Arrays. sort() method.


2 Answers

Here's one approach. It copies the defined array elements to a new array and saves their indexes. It sorts the new array and then puts the sorted results back into the indexes that were previously used.

var a = []; a[0] = 3; a[1] = 2;  a[2] = 6;  a[7] = 4;  a[8] = 5;   // sortFn is optional array sort callback function,  // defaults to numeric sort if not passed function sortSparseArray(arr, sortFn) {     var tempArr = [], indexes = [];     for (var i = 0; i < arr.length; i++) {         // find all array elements that are not undefined         if (arr[i] !== undefined) {             tempArr.push(arr[i]);    // save value             indexes.push(i);         // save index         }     }     // sort values (numeric sort by default)     if (!sortFn) {         sortFn = function(a,b) {             return(a - b);         }     }     tempArr.sort(sortFn);     // put sorted values back into the indexes in the original array that were used     for (var i = 0; i < indexes.length; i++) {         arr[indexes[i]] = tempArr[i];     }     return(arr); } 

Working demo: http://jsfiddle.net/jfriend00/3ank4/

like image 134
jfriend00 Avatar answered Oct 03 '22 23:10

jfriend00


You can

  1. Use filter or Object.values to obtain an array with the values of your sparse array.
  2. Then sort that array, from largest to smaller. Be aware it's not stable, which may be specially problematic if some values are not numeric. You can use your own sorting implementation.
  3. Use map and pop to obtain the desired array. Assign it to a.
var b = a.filter(function(x) {     return true; }).sort(function(x,y) {     return y - x; }); a = a.map([].pop, b); 

Or, in ECMAScript 2017,

a = a.map([].pop, Object.values(a).sort((x,y) => y-x)); 
like image 41
Oriol Avatar answered Oct 04 '22 00:10

Oriol