码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-01-11 00:09:52      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:int   中文字符串   元素   阿里   打印   ons   turn   结果   add   

类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的元素通过中文字符串排序

标签:int   中文字符串   元素   阿里   打印   ons   turn   结果   add   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!