My problem: I have an array of objects which I sort ASC DESC by one of the objects keys, after that I need to sort an array of string that will be sorted the way the object array has been sort. example:
arrayOfObjects = [{name:"john",nuber:6,food:"pizza"},
{name:"david",nuber:2,food:"gulash"},
{name:"margaret",nuber:7,food:"gugi barries"}]
arrayOfStrings = ['r1','r2','r3']
so each object in arrayOfObjects has its own string. so say, john has r1, and they are both first indexed, when I sort by number john goes second, and I want hes number to go second as well (as well as david and margaret numbers)
I need to re-arrange the arrayOfStrings the same way (I dont care what it was sorted by) the arrayOfObjects was sorted
My sorting function :
dataArray.sort(dynamicSort(sortBy));
function dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
if(direction=='asc'){
var c = b;
b=a;
a=c;
}
var result = ( b[property] < a[property]) ? -1 : ( b[property]> a[property]) ? 1 : 0;
return result * sortOrder;
}
}
{name:"john", r:'r1' ...and then retrieve these values when the array is sorted.