集合对象根据某个字段首字母排序

时间:2021-03-20 15:00:58
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Locale;

    /**
     * 排序后的对象集合
     * @param list
     * @return
     */
    public List<InsurersBean> listToSortByName(List<InsurersBean> list){
        if(list==null || list.size()==0){
            return null;
        }
        Map<String, InsurersBean> map = new HashMap<String, InsurersBean>();
        String names[] = new String[list.size()];
        for(int i=0;i<list.size();i++){
            String name = list.get(i).getName_eng();
            names[i] = name;
            map.put(name, list.get(i));
        }

        Comparator<Object> comparator = Collator.getInstance(Locale.ENGLISH);//中文改成:CHINA
        Arrays.sort(names, comparator);
        
        list.clear();
        for(String name : names){
            if(map.containsKey(name)) {
                list.add(map.get(name));
            }
        }
        return list;
    }

InsurersBean 自己的集合对象,根据 Name_eng 首字母从 A-Z 排序!