如何对JQUERY中以非数字字符开头的数组的数组列表进行排序

How to Sort/Ordering of array list of array that start with the charecter not numeric in JQUERY?

本文关键字:数组 列表 排序 开头 数字字符 JQUERY      更新时间:2023-09-26

我有一个数组:

data =["c3.2xlarge","c3.4xlarge","c3.large"];
data= [ "c3.4xLarge", "c3.2xLarge", "c3.8xLarge", "c3.Large",
        "d3.4xLarge", "d3.2xLarge", "d3.8xLarge", "d3.Large", 
        "t2.4xLarge", "t2.2xLarge", "t2.8xLarge", "t2.Large"
      ];

它现在是按字母顺序排列的,但这给了我们真正希望c3.large首先出现的时间。

期望输出=["c3.Lage","c3.2xLarge","c3.4xLarge","c3.8xLarge,"d3.大","d3.2x大","t2.Lage"、"t2.2xLarge"、"t2.4xLarge];

如何创建一个函数来对对象进行排序?

重写js数组排序方法;希望这能帮助

var data = ["c3.2xlarge","c3.4xlarge","c3.large"];
    console.log(data.sort(function (a,b) {
        var aSub = a.substring(3,4),
        bSub = b.substring(3,4);
    if(isNaN(aSub)){
        return -1;
    }
    else if(isNaN(bSub)){
        return 1;
    }
    else{
        return aSub > bSub ? 1:-1;
    }
}));

 var data = [ "c3.4xLarge", "c3.2xLarge", "c3.8xLarge", "c3.Large", "d3.4xLarge", "d3.2xLarge", "d3.8xLarge", "d3.Large", "t2.4xLarge", "t2.2xLarge", "t2.8xLarge", "t2.Large", ];

 data.sort(function (a,b) {
    var firA = a.substring(0,1),
        firB = b.substring(0,1);
        // console.log(firA);
        // console.log(firB);
    if(firA != firB){
        return firA >firB ? 1 : -1;
    }
    var aSub = a.substring(3,4),
    bSub = b.substring(3,4);
    if(isNaN(aSub)){
        return -1;
    }
    else if(isNaN(bSub)){
        return 1;
    }
    else{
        return aSub > bSub ? 1:-1;
    }
})
 console.log(data);