zoukankan      html  css  js  c++  java
  • 将List的元素通过中文字符串排序

    类customer

    public class Customer {
    	
    	public String name;
    	public int age;
    	
    	Customer(String name, int age){
    		this.name = name;
    		this.age = age;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}	
    }
    

      

    排序

    public class main {
        public static void main(String[] args) {
            
            List<Customer> customers = new ArrayList<Customer>();
            List<Customer> customers2 = new ArrayList<Customer>();
            customers.add(new Customer("拉克丝", 12));
            customers.add(new Customer("团藏", 12));
            customers.add(new Customer("布拉格", 15));
            customers.add(new Customer("尼古拉斯赵四", 12));
            customers.add(new Customer("狗娃", 12));
            customers.add(new Customer("阿里郎", 15));
            customers.add(new Customer("雅蠛蝶", 15));
           
            for (Customer customer:customers) {
                customers2.add(customer);
                
               Collections.sort(customers2, new Comparator<Customer>(){
                  @Override
                  public int compare(Customer o1, Customer o2){
                    return Collator.getInstance(Locale.CHINESE).compare(o1.getName(), o2.getName());
                  }
               });
            }
            
            //打印
            for (Customer customer:customers2) {
                System.out.println(customer.getName());
            }
        }
        
        
    }

    打印结果

    阿里郎
    布拉格
    狗娃
    拉克丝
    尼古拉斯赵四
    团藏
    雅蠛蝶

     ---------------------------- 打赏 -----------------------------------

  • 相关阅读:
    java8
    java7
    java6
    java5
    java复习4
    学习笔记
    Reflection笔记
    通过Reflection来获得方法和信息
    學習反射2
    學習反射1
  • 原文地址:https://www.cnblogs.com/Jomini/p/10252964.html
Copyright © 2011-2022 走看看