I have the following array:
var times = [
["04/11/10", "86kg"],
["05/12/11", "90kg"],
["06/12/11", "89kg"]
];
I want to list these dates and their corresponding weights in ascending order.
I know you can sort arrays with sort and I found the following function from this About.com page which I thought would do what I wanted:
times.sort(dmyOrdA);
var dateRE = /^(\d{2})[\/\-](\d{2})[\/\-](\d{2})/;
function dmyOrdA(a,b) {
a = a.replace(dateRE, "$3$2$1");
b = b.replace(dateRE, "$3$2$1");
if (a > b) {
return 1;
}
else if (a < b) {
return -1;
}
else {
return 0;
}
}
However, using this function gives me the following error:
a.replace is not a function
Is anyone able to help with my query?
Thanks in advance.
EDIT:
Looking at a previous stack overflow question it seems as if in my case 'a' is not a string. However, I don't understand why this is so.
timesarray stores 3 arrays in your example, not strings (like["04/11/10", "86kg"]). So with the sorting function,aandbwill be two arrays.