首页 > 编程语言 > 详细

将List的元素通过中文字符串排序

时间:2019-01-10 23:50:39      阅读:237      评论:0      收藏:0      [点我收藏+]

类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());
        }
    }
    
    
}

打印结果

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

 

将List的元素通过中文字符串排序

原文:https://www.cnblogs.com/Jomini/p/10252964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!