admin管理员组文章数量:1302502
I need to sort a list in angular, alphabetically(ascending) but want the special characters if any prefixed to an item to be pushed at the end of the list. For e.g: the list should like:
Apple
Banana
*Apple
any suggestions would be remended.
I need to sort a list in angular, alphabetically(ascending) but want the special characters if any prefixed to an item to be pushed at the end of the list. For e.g: the list should like:
Apple
Banana
*Apple
any suggestions would be remended.
Share edited Aug 7, 2015 at 3:42 Arun P Johny 388k68 gold badges531 silver badges532 bronze badges asked Aug 7, 2015 at 3:40 Preena KhanujaPreena Khanuja 2052 gold badges6 silver badges14 bronze badges 4- Possible duplicate of stackoverflow./questions/5503900/… – dpanshu Commented Aug 7, 2015 at 3:46
- 2 Not a duplicate, but not that far off... – nageeb Commented Aug 7, 2015 at 4:00
-
Just use
sort()
callback function and addz
on strings starting with special chars, check my answer. – Walter Chapilliquen - wZVanG Commented Aug 7, 2015 at 6:30 - @AndyRay thnxx for the awsome solution :) – Preena Khanuja Commented Aug 7, 2015 at 10:02
4 Answers
Reset to default 4Here's a fairly simple solution. When manually paring strings it's good practice to use localeCompare which will correctly sort even when the user's language specific locale dictates a different sort order. But that function alone won't solve our problem. Building on top of @wZVanG's clever answer, we'll replace any non-word characters, using the \W
regex character group, at the beginning of the string with the letter z
which will automatically sort them to the end of the list.
Note one flaw in this is that if any of your words start with more than one z
they will get sorted after the special characters. A simple workaround is to add more z
s to the string, as in return a.replace(/^\W+/, 'zzz').localeCompare(b.replace(/^\W+/, 'zzz')
.
var array = ["Banana", "Apple", "*Canana", "Blackberry", "Banana", "*Banana", "*Apple"];
array.sort(function(a,b) {
return a.replace(/^\W+/, 'z').localeCompare(b.replace(/^\W+/, 'z'));
});
This is probably not correct but its the best I can think of at this hour.
DEMO
var array = ["Apple", "Banana", "*Apple"];
// Split the arrays with and without special chars
var alphaNumeric = array.filter(function (val) {
return !(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(val));
});
var specialChars = array.filter(function (val) {
return /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(val);
});
console.log(alphaNumeric, specialChars);
// Sort them individually
alphaNumeric.sort();
specialChars.sort();
// Bring them back together but put the special characters one afterwards
var both = alphaNumeric.concat(specialChars);
console.log(both);
var list = ["Apple","Orange", "Banana", "*Banana","*Apple"];
regex= /^[A-z]+$/;
dummyArray1=[];
dummyArray2=[];
for(var i =0;i< list.length; i++){
if(regex.test(list[i][0])){
dummyArray1.push(list[i]);
}
else{
dummyArray2.push(list[i]);
}
}
console.log(dummyArray1.sort());
console.log(dummyArray2.sort());
console.log(dummyArray1.concat(dummyArray2));
One line:
var sorted = ["Banana", "Apple", "*Canana", "Blackberry", "/Banana", "*Banana", "*Apple"]
.sort(function(a, b, r){
return r = /^[a-z]/i, (r.test(a) ? a : "z" + a) > (r.test(b) ? b : "z" + b)
});
//Test
document.write(sorted.join("<br />"));
Add Z
if the item does not begin with a letter of the alphabet.
本文标签: javascriptSort a list alphabetically with characters at the endStack Overflow
版权声明:本文标题:javascript - Sort a list alphabetically with characters at the end - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741680340a2392132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论