4

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.

1
  • Because your times array stores 3 arrays in your example, not strings (like ["04/11/10", "86kg"]). So with the sorting function, a and b will be two arrays. Commented May 30, 2011 at 16:41

2 Answers 2

3

when sorting an array, the parameters that the sort function takes are array elements.
In your case, you array elements are also arrays... bummer.
You want to sort the array of arrays by the first element of each element (tricky).
So just change the a and b to a[0] and b[0] :

function dmyOrdA(a,b) {
    a = a[0].replace(dateRE, "$3$2$1");
    b = b[0].replace(dateRE, "$3$2$1");
    if (a > b)
        return 1;
    else if (a < b)
        return -1;
    else
        return 0;
}

You got a.replace is not a function because replace is a String method and you tried to apply it to an Array

Sign up to request clarification or add additional context in comments.

8 Comments

Great thanks Gion that works perfectly. To be honest the sort function completely baffles me. I'm passing 'a' and 'b' as parameters to the 'dmyOrdA' function but I can't see what relationship this has to my 'times' array. If you had time it would be great if you could explain this. Thanks.
@Stephen Young: When you sort an array in javascript, you need to provide a way of comparison between two of the array's elements. This way, you pass as a parameter to the sort method a function that takes 2 elements of your array and decides which one will be lower and which one will be greater.If the first element is grater than the other, the function will return 1.If it is lower than the other, it will return -1, and if they're equal, it returns 0. You can picture the function as a question: is the first element bigger than the second?Answer:1-yes,-1-no,0-inconclusive
@gion_13 I think I'm beginning to understand this. Just two questions: (1) You say "you pass as a parameter to the sort method a function that takes 2 elements of your array" but how does the sort method know how to sort all the elements in the array? (2) I don't understand the return aspect of the function. If you take out the else if part and change if (a > b) return 1 to if (a > b) return -1 it will still sort by ascending order. However, if you leave out the (a > b) statement and just have if (a < b) return -1 it will not sort by descending order but return 1 will. Why?
(1)It depends on the browser implementation, but i think that the sort method applies the given function recursively on pairs of elements.In order to sort a list, you must-at a certain point- compare two elements;and your function tells the comparison logic.For sorting algorithms see en.wikipedia.org/wiki/Sorting_algorithm.(2)If you change one if,change the other one too;) You must always have a return value for each comparison case, so that the algorithm would know how to alter the list. If you try something like [4,3,2,1].sort(function(a,b){return -1;}),you won't get a sorted list.
I'm nearly there now!! The Mozilla docs (developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…) says "If compareFunction(a, b) is greater than 0, sort b to a lower index than a." In that case if you do if (a > b) return -1 then surely a should be sorted to a lower index than b? But if you do this nothing is sorted.
|
0

You should convert to proper dates and then subtract in the compare function:

function dmyOrdA(a,b){return myDate(a[0]) - myDate(b[0]);}
function myDate(s){var a=s.split("/"); return new Date("20"+a[2],a[1]-1,a[0]);}

2 Comments

Thanks for this. I was wondering how to do that. Why do you put -1 after a[1]?
The month parameter is 0 based; the day is 1 based

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.