我今天写了这个并且工作正常,但是,我注意到大写在排序方面优先考虑...如何在保留原始字符串的同时对这个不区分大小写进行排序?
// original data
var data = 'keyword,another,test,546,Hello';
//if keywords then split them by comma into an array
var keyArray = new Array();
keyArray = data.split(",");
// sort the new array alpha
keyArray.sort();
// now output as nice display
var keyOut = '';
var keyLength = keyArray.length;
for (var i = 0; i < keyLength; i++) {
//create output
var keyOut = keyOut.concat('<span class="label label-default">'+keyArray[i]+'</span> ');
}
return keyOut;
编辑:
在找到一个快速示例并按预期工作后回答了我自己......
keyArray.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
答案 0 :(得分:0)
您可以这样做:
// original data
var data = 'keyword,another,test,546,Hello';
//if keywords then split them by comma into an array
var keyArray = new Array();
keyArray = data.split(",");
// sort the new array
keyArray.sort(function(a, b) {
var textA = a.toUpperCase();
var textB = b.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
// now output as nice display
var keyOut = '';
var keyLength = keyArray.length;
for (var i = 0; i < keyLength; i++) {
//create output
var keyOut = keyOut.concat('<span class="label label-default">'+keyArray[i]+'</span> ');
}
return keyOut;