How to sort an array in javascript, but save results in separate array -


i want sort array of objects based upon of object properties. want original array left unchanged. instead, want save sort order of indexes in separate array.

var source = [   {"a": 2, "b": 8, "c": 9},   {"a": 4, "b": 3, "c": 7},     {"a": 1, "b": 0, "c": 6} ]  var sortedindexes;  somesortofsortmethod("a", "asc");  // result of sortedindexes containing indexes source array: // [2, 0, 1]  

any ideas how this? can't use built in javascript sort method because changes source. need capture sort , save order indexes source arry.

var source = [       {"a": 2, "b": 8, "c": 9},       {"a": 4, "b": 3, "c": 7},         {"a": 1, "b": 0, "c": 6}     ];  var orderedcopyarray = _.sortby(source, "a");  // defualt ascending console.log(json.stringify(orderedcopyarray));  // descending console.log(json.stringify(orderedcopyarray.reverse()));  var indexesarray = [], leng = source.length;  // descending array ordered var reverse = orderedcopyarray.reverse();  // index for(var i=0; < leng; i++){   var obj1 = reverse[i];    for(var j=0; j < leng; j++){     var obj2 = source[j];     if(_.isequal(obj1, obj2)){       indexesarray.push(j);       break;     }   } }  console.log(indexesarray); //[2, 0, 1] 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -