實體類
import java.text.Collator;
import java.util.Locale;
public class Language implements Comparable<Language>{
private String language;
private String abbreviation;
public Language(String language,String abbreviation){
this.language = language;
this.abbreviation = abbreviation;
}
public String getAbbreviation() {
return abbreviation;
}
public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@Override
public int compareTo(Language another) {
Collator c = Collator.getInstance(Locale.CHINA);
return c.compare(this.language, another.getLanguage());
}
@Override
public String toString() {
return "Language [language=" + language + ", abbreviation=" + abbreviation + "]";
}
}
類實作Comparable接口,重寫compareTo方法
寫個測試類
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
public class Test {
public static void main(String[] args) {
ArrayList<Language> mLanguageList = new ArrayList<Language>();
mLanguageList = new ArrayList<Language>();
mLanguageList.add(new Language("日語", "jp"));
mLanguageList.add(new Language("西班牙語", "spa"));
mLanguageList.add(new Language("泰語", "th"));
mLanguageList.add(new Language("俄羅斯語", "ru"));
mLanguageList.add(new Language("德語", "de"));
mLanguageList.add(new Language("荷蘭語", "nl"));
mLanguageList.add(new Language("英語", "en"));
mLanguageList.add(new Language("韓語", "kor"));
mLanguageList.add(new Language("法語", "fra"));
mLanguageList.add(new Language("阿拉伯語", "ara"));
mLanguageList.add(new Language("葡萄牙語", "pt"));
mLanguageList.add(new Language("意大利語", "it"));
mLanguageList.add(new Language("希李語", "el"));
mLanguageList.add(new Language("希臘一", "el"));
mLanguageList.add(new Language("希臘語", "el"));
Collections.sort(mLanguageList);
System.out.println(mLanguageList);
}
}