I have an array of indexes, called indexSelected. There is also an array of objects called weather. I have put all of the Site Names that that I am dealing with into an array called stationName. I want to sort this array alphabetically whilst keeping the original indexes for that I can refer back to the other properties associated with object. I have tried the method below, however, I cannot seem to get it to work.
var stationName=[];
for (var i=0; i<indexSelected.length; i++) {
stationName.push(weather[indexSelected[i]]["Site Name"]);
}
var copyStationName = stationName.slice(0)
var sortedStationName = stationName.sort();
var originalIndex=[];
for (var i=0; i<stationName.length; i++) {
originalIndex.push(copyStationName.indexOf(sortedStationName[i]))
}
var station=[];
for (var i=0; i<indexSelected.length; i++) {
station.push(weather[originalIndex[i]]["Site Name"]);
}
This station array was to check that the originalIndexes array was correct. It isn't, and I don't know why. I would appreciate some help, or another way to sort this array. I'm looking to put all of the data into a table, sort alphabetically by site name. Thanks
asort()in PHP. There's a JS version of that here.