Skip to content Skip to sidebar Skip to footer

Javascript Sort Placing Middle Item At Top

I have the following sort function: var timeArray = new Array('11:41', '11.39', '11:41', '11:41', '11:40', '11:70', '11:39', '11:38', '11:38', '11:37', '11:37'); timeArray.sort(fu

Solution 1:

Change the sort function to

timeArray.sort(function(c, d) {
    var digit1 = parseInt(c.replace(/\D/g,''));
    var digit2 = parseInt(d.replace(/\D/g,''));
    return  digit1 - digit2;
});

FIDDLE

It's expecting a number to be returned, not a boolean.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort


Post a Comment for "Javascript Sort Placing Middle Item At Top"