sort array with integer strings type in jQuery

后端 未结 5 1354
后悔当初
后悔当初 2020-12-10 19:25

I have a array of integers of type string.

var a = [\'200\',\'1\',\'40\',\'0\',\'3\'];

output

>>> var a = [\'2         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 20:03

    Thanks all, though I dont know jQuery much, but from you guys examples, I summarized the code as follows which works as per my requirement

    to be used in firebug

    var data = ['10','2', 'apple', 'c' ,'1', '200', 'a'], temp;
    temp = data.sort(function(a,b) {
             var an = +a;
             var bn = +b;
              
             if (!isNaN(an) && !isNaN(bn)) {
                 return an - bn;
             } 
             return ab ? 1 : 0;
         }) ;
    alert(temp);
    

提交回复
热议问题