类似章节(1,1.1,1.1.1)数字字符串的排序

var a = [
    "1",
    "1.1",
    "1.1.1",
    "1.1.2",
    "1.2",
    "1.3",
    "1.4",
    "1.5",
    "2",
    "2.1",
    "2.1.1",
    "2.1.2",
    "2.2",
    "2.11.2",
    "2.11.11",
    "10.2.1",
    "10.2.2",
    "10.2.11",
    "10.11.1",
    "11.1.1",
    "11.1.2"
]

//默认字符串排序(按位从左到右比较的)
alert(a.sort().join("\r\n"));

//自定义排序方法(按大小排序)
alert(a.sort(function(a,b){
    return a.split(".")[0] - b.split(".")[0];
}).join("\r\n"));

参考阅读:http://www.w3school.com.cn/js/jsref_sort.asp

posted @ 2013-03-22 13:58  Jarno  阅读(365)  评论(0编辑  收藏  举报