Ⅰ java中怎麼對英文字母進行排序
在java中對英文字母進行比較的時候,在java虛擬機中,他首先會轉換成ASII來進行比較,所以可以直接寫,如果是大寫和小寫的比較的話,大寫要加上一定的數字,才能進行比較,如果嫌麻煩,你也可以將大寫轉化成小寫進行比較,採用冒泡法排序吧
Ⅱ java中怎樣把輸入的基本英文單詞按首字母順序排列
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.List;
/**
*規則:大寫字母前於小寫字母
*
*@authorAdministrator
*
*/
publicclassSortWords{
privatestaticfinalString[]array={"A","B","C","D","E","F","G",
"H","I","J","K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z"};
publicstaticvoidmain(String[]args){
SortWordssw=newSortWords();
List<String>words=newArrayList<String>();
words.add("book");
words.add("ear");
words.add("alert");
words.add("choice");
words.add("name");
words.add("memery");
words.add("fight");
words.add("call");
words.add("joy");
List<String>list=sw.sort(words,"forward");
for(Stringstr:list){
System.out.println(str);
}
}
publicList<String>sort(List<String>words,Stringtype){
List<Word>list=newArrayList<Word>();
List<String>res=newArrayList<String>();
for(Stringword:words){
Wordw=indexNum(word);
list.add(w);
}
switch(type){
case"forward":
Collections.sort(list,newForward());
for(Wordw:list){
res.add(w.getContent());
}
break;
case"back":
Collections.sort(list,newBack());
for(Wordw:list){
res.add(w.getContent());
}
break;
}
returnres;
}
publicWordindexNum(Stringword){
String[]letters=word.split("");
Stringres="";
Wordw=newWord();
Stringletter=word.substring(0,1);
for(inti=0;i<array.length;i++){
Stringpattern=array[i];
if(letter.equalsIgnoreCase(pattern)){
res+=i;
break;
}
}
w.setContent(word);
w.setIndexNum(Integer.parseInt(res));
returnw;
}
{
@Override
publicintcompare(Objectarg0,Objectarg1){
Wordw1=(Word)arg0;
Wordw2=(Word)arg1;
intflag=w1.getIndexNum()-w2.getIndexNum();
returnflag;
}
}
{
@Override
publicintcompare(Objectarg0,Objectarg1){
Wordw1=(Word)arg1;
Wordw2=(Word)arg0;
intflag=w1.getIndexNum()-w2.getIndexNum();
returnflag;
}
}
}
classWord{
privateStringcontent;
privateIntegerindexNum;
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(Stringcontent){
this.content=content;
}
publicIntegergetIndexNum(){
returnindexNum;
}
publicvoidsetIndexNum(IntegerindexNum){
this.indexNum=indexNum;
}
}
Ⅲ java中字母怎麼按大小排序
java中字母排序是按照字典順序的,可以直接使用Arrays.sort()
方法對字母數組進行排序即可