java中的中文字符串排序

实体类

import java.text.Collator;
import java.util.Locale;

public class Language implements Comparable{
    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 mLanguageList = new ArrayList();
		mLanguageList = new ArrayList();
        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);
	}
	
}







你可能感兴趣的:(android/java,skill)