汉字的排序,如果含有英文的话,各排个的,互补影响。
<body>
<select class="sel" id="ect">
<option value="蛋蛋">蛋蛋</option>
<option value="笨笨">笨笨</option>
<option value="小明">小明</option>
<option value="笨蛋">笨蛋</option>
<option value="汐">汐</option>
<option value="Jack">Jack</option>
<option value="Daniel">Daniel</option>
<option value="克杰">克杰</option>
<option value="信宗">信宗</option>
<option value="fido">fido</option>
<option value="tom">tom</option>
<option value="jerry">jerry</option>
</select>
<input type="submit" value="排序" onClick="order()"/>
</body>
javascript的部分:
<script src="jquery-1.11.2.js"></script>
<script type="text/javascript">
function order(){
$("select option").sort(function(a,b){
var aText = $(a).text();
var bText = $(b).text();
if(typeof aText =="string" && typeof bText == "string"){
return aText.localeCompare(bText);
}
}).appendTo('select') ;
}
</script>
注:localeCompare() 方法用于字符串编码的排序
sort() 方法用于数组元素的排序
通讯录排序:
function pySegSort(arr,empty) {
if(!String.prototype.localeCompare)
return null;
var letters = "*ABCDEFGHJKLMNOPQRSTWXYZ".split('');
var zh = "阿八嚓哒妸发旮哈讥咔垃麻拏噢妑七呥扨它穵夕丫帀".split('');
var segs = [];
var curr;
$.each(letters, function(i,n){
curr = {letter: this, data:[]};
$.each(arr, function(k,v) {
if((!zh[i-1] || zh[i-1].localeCompare(v.text) <= 0) && v.text.localeCompare(zh[i]) == -1) {
curr.data.push(this);
}
});
if(empty || curr.data.length) {
py.push(this);
segs.push(curr);
curr.data.sort(function(a,b){
return a.text.localeCompare(b.text);
});
}
});
return segs;
}