javascript排序不能正确使用字符串

javascript排序不能正确使用字符串,javascript,sorting,Javascript,Sorting,我使用JavaScript代码对页面元素输入中的字符串进行排序,如下所示: a=document.getElementsByTagName("textarea"); input=a[0].innerHTML; input=input.split(','); input=input.sort(); alert(input); var str=input.join(); str=str.replace(" ",""); 输入字符串有逗号和空格,因此如果输入类似: yet, must, has, w

我使用JavaScript代码对页面元素输入中的字符串进行排序,如下所示:

a=document.getElementsByTagName("textarea");
input=a[0].innerHTML;
input=input.split(',');
input=input.sort();
alert(input);
var str=input.join();
str=str.replace(" ","");
输入字符串有逗号和空格,因此如果输入类似:

yet, must, has, wants, would, some, are, let, own, can, could, which, his, had, got, our, only, also, every, after, other, may, you, them, while, ever, what, get, its, why, their, her, him, just, say, this, than, have, able, least, like, whom, nor, cannot, into, among
输出为:

able, after, also, among, are, can, cannot, could, ever, every, get, got, had, has, have, her, him, his, into, its, just, least, let, like, may, must, nor, only, other, our, own, say, some, than, their, them, this, wants, what, which, while, whom, why, would, you,yet

你可以注意到它应该在你面前,所以有什么不对???

你需要用空格分开

input = input.split(', ');
否则,字符串的开头有一个空格

var-input='然而,必须、有、想要、会、一些、是、让、拥有、能、能、其中、他的、有、得到、我们的、唯一的、也、每个、之后、其他、可能、你、他们、while、while、ever、what、what、get、its、why、the、him、just、say、than、have、able、least、like、who、not、not、into、into、in';
console.log(input.split(',).sort())

.as控制台包装{max height:100%!important;top:0;}
您需要用空格分隔

input = input.split(', ');
否则,字符串的开头有一个空格

var-input='然而,必须、有、想要、会、一些、是、让、拥有、能、能、其中、他的、有、得到、我们的、唯一的、也、每个、之后、其他、可能、你、他们、while、while、ever、what、what、get、its、why、the、him、just、say、than、have、able、least、like、who、not、not、into、into、in';
console.log(input.split(',).sort())

。作为控制台包装{max height:100%!important;top:0;}
排序前应删除空格:

var-input='然而,必须、有、想要、会、一些、是、让、拥有、能、能、其中、他的、有、得到、我们的、唯一的、也、每个、之后、其他、可能、你、他们、while、while、ever、what、what、get、its、why、the、him、just、say、than、have、able、least、like、who、not、not、into、into、in';
input=input.replace(/\s+/g',).split(',).sort();

控制台日志(输入)排序前应删除空格:

var-input='然而,必须、有、想要、会、一些、是、让、拥有、能、能、其中、他的、有、得到、我们的、唯一的、也、每个、之后、其他、可能、你、他们、while、while、ever、what、what、get、its、why、the、him、just、say、than、have、able、least、like、who、not、not、into、into、in';
input=input.replace(/\s+/g',).split(',).sort();

控制台日志(输入)太棒了,为什么否决?太棒了,为什么否决?它是一个正则表达式,可以替换字符串中的空格,你可以在谷歌上找到。它是一个正则表达式,可以替换字符串中的空格,你可以在谷歌上找到。