javascript sort placing middle item at top

后端 未结 1 1567
心在旅途
心在旅途 2021-01-26 11:15

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\',          


        
相关标签:
1条回答
  • 2021-01-26 11:43

    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

    0 讨论(0)
提交回复
热议问题