Home > Web Front-end > JS Tutorial > js code to sort strings in specific alphabetical order_javascript skills

js code to sort strings in specific alphabetical order_javascript skills

WBOY
Release: 2016-05-16 17:01:56
Original
1475 people have browsed it

For example, given a specific alphabetical order, "a", "o", "e", "u", "y", "i", please put 'aiye', 'oeu', 'ayo', 'eoyiu' ,'yuo','you','iao', the seven strings are sorted in the given alphabetical order.

My idea is to equate the given 6 letters into numbers from 0-5, and then replace the letters in the 7 strings with numbers from 0-5, and then Use the sort method to sort, the code is as follows:

Copy code The code is as follows:

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);

Copy code The code is as follows:

</pre&gt ;<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&gt ;<pre code_snippet_id="173179" snippet_file_name="blog_20140127_1_4210131">

 
 
< pre>
 
 
  
 



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template