Ⅰ 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()
方法对字母数组进行排序即可