0

I am having issues on identifying the actually index of the array its being sorted.

My list is a bit messy:

    masterList =[];
    masterList.push([1,"NY","New York, NY 10036, United States",40.760262,-73.993286,"07/30/2015"]);
    masterList.push([2,"Chgo","Chicago, IL, United States",41.878113,-87.629799,"07/06/2015"]);
    masterList.push([3,"Japan","Japan",11.1111,-11.1111,"07/22/2015"]);
    masterList.push([4,"China","China",22.2222,-22.2222,"07/18/2015"]);

masterList.sort(sortdates());

function sortdates(a,b) {
        return function(a, b){
                a = Date(a[5]);
                console.log(a);
                b = Date(b[5]);
                return a - b;
            };
}

Obviously this isn't working. I want to be able to grab the 6st index on the multidimensional array and sort by that.

I tried looking everywhere.. but I don't have a "key" that ties to my array.. so I can't really use that to point it. Any suggestions?

1
  • 1
    Instead of masterList.sort(sortdates()) you have to write masterList.sort(sortdates). You have to provide a reference to a function, not call the sort-function. Commented Jul 30, 2015 at 15:30

3 Answers 3

1

There are three problems. First, you should pass a function handle to the sort function. You are calling the function instead. Second, the function should not return a function and should accept two parameters. Third and most important, you need to create new instances of the date object. It's not a static method.

The returning a function could work, so the real problem you have is the lack of the new keyword

    masterList =[];
    masterList.push([1,"NY","New York, NY 10036, United States",40.760262,-73.993286,"07/30/2015"]);
    masterList.push([2,"Chgo","Chicago, IL, United States",41.878113,-87.629799,"07/06/2015"]);
    masterList.push([3,"Japan","Japan",11.1111,-11.1111,"07/22/2015"]);
    masterList.push([4,"China","China",22.2222,-22.2222,"07/18/2015"]);

masterList.sort(sortdates);

function sortdates(a,b) {
                a = new Date(a[5]);
                console.log(a);
                b = new Date(b[5]);
                return a - b;
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks! hmm I put in console.log(masterList), but nothing is happening.. the array doesn't seemed to be sorted?
@SQLNub It seems to sort. I'll put together a better demo.
it is sorted! ya i think i forgot the new part. Thank you!
@SQLNub aw dang it... I just finished it too. :P jsfiddle.net/nrf5e8hc Oh well. Glad the issue is resolved.
1

Looks like you've got an extra function in there. This would work better, no need to return another function.

function sortdates(a,b) {
    a = new Date(a[5]);
    console.log(a);
    b = new Date(b[5]);
    return a - b;
}

Then you can pass sortdates (without the ()) to sort

1 Comment

I just tried using your suggested code in combination of without (), then console.log(masterList); shortly after .sort... doesn't seem to be reflecting the new array?
-1

How about using underscore.js?

Then you can use sortBy as follows:

var sorted = _.sortBy(masterList, function(value){ return new Date(value[5]).getTime(); });

Here is a fiddle with the solution

Comments

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.