例如,给一个特定的字母顺序,"a","o","e","u","y","i",请把'aiye','oeu','ayo','eoyiu','yuo','you','iao'这7个字符串按照给定的字母顺序排序。
我的思路是把那几个给定的6个字母分别等效成0-5的数字,然后把那7个字符串中的字母替换成0-5中的数字,再用sort方法进行排序,代码如下:
复制代码 代码如下:
var word = ['aiye','oeu','ayo','eoyiu','yuo','you','iao'];
var obj = {a:0,o:1,e:2,u:3,y:4,i:5};
var obj1 = {0:'a',1:'o',2:'e',3:'u',4:'y',5:'i'};
for(var index in word){
var word1 = word[index];
var word3 = "";
for(var i = 0; i< word1.length; i ++ ){
word3 += obj[word1[i]];
};
word[index] = word3
}
word.sort();
for(var index in word){
var word1 = word[index];
var word3 = "";
for(var i = 0; i< word1.length; i ++ ){
word3 += obj1[word1[i]];
};
word[index] = word3
}
console.log(word);
复制代码 代码如下:
<pre code_snippet_id="173179" snippet_file_name="blog_20140127_1_4210131"></pre><pre code_snippet_id="173179" snippet_file_name="blog_20140127_1_4210131" name="code" class="html"><span class="source-code" style="font-family:Consolas,'Lucida Console',monospace; white-space:pre-wrap"><span style="font-family:Consolas,'Lucida Console',monospace; white-space:pre-wrap"></span></span><pre code_snippet_id="173179" snippet_file_name="blog_20140127_1_4210131"></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
</pre>
我的思路是把那几个给定的6个字母分别等效成0-5的数字,然后把那7个字符串中的字母替换成0-5中的数字,再用sort方法进行排序,代码如下:
复制代码 代码如下:
var word = ['aiye','oeu','ayo','eoyiu','yuo','you','iao'];
var obj = {a:0,o:1,e:2,u:3,y:4,i:5};
var obj1 = {0:'a',1:'o',2:'e',3:'u',4:'y',5:'i'};
for(var index in word){
var word1 = word[index];
var word3 = "";
for(var i = 0; i< word1.length; i ++ ){
word3 += obj[word1[i]];
};
word[index] = word3
}
word.sort();
for(var index in word){
var word1 = word[index];
var word3 = "";
for(var i = 0; i< word1.length; i ++ ){
word3 += obj1[word1[i]];
};
word[index] = word3
}
console.log(word);
复制代码 代码如下:
<pre code_snippet_id="173179" snippet_file_name="blog_20140127_1_4210131"></pre><pre code_snippet_id="173179" snippet_file_name="blog_20140127_1_4210131" name="code" class="html"><span class="source-code" style="font-family:Consolas,'Lucida Console',monospace; white-space:pre-wrap"><span style="font-family:Consolas,'Lucida Console',monospace; white-space:pre-wrap"></span></span><pre code_snippet_id="173179" snippet_file_name="blog_20140127_1_4210131"></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
</pre>
标签:
字符串,字母顺序,排序
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“把字符串按照特定的字母顺序进行排序的js代码”评论...