zoukankan      html  css  js  c++  java
  • 有关Collection中sort排序

    Collections中sort()方法如果比较的是对象

    package com.huan.yu;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    
    
    public class Demo {
    
    	public static void main(String[] args) {
    		List<CustomerOrder> customerOrders = new ArrayList();
    
    		Map map = new HashMap();
    
    		CustomerOrder customerOrder = new CustomerOrder((long) 1,"huanyu1", "xiaozhang", "xiaoli", 1);
    		CustomerOrder customerOrder1 = new CustomerOrder((long) 2,"huanyu1", "xiaozhang", "xiaoli", 4);
    		CustomerOrder customerOrder2 = new CustomerOrder((long) 3,"huanyu1", "xiaozhang", "xiaoli", 5);
    
    		CustomerOrder customerOrder3 = new CustomerOrder((long) 4,"huanyu", "xiaozhang", "xiaoli", 6);
    		CustomerOrder customerOrder4 = new CustomerOrder((long) 5,"huanyu", "xiaozhang", "xiaoli", 7);
    		CustomerOrder customerOrder5 = new CustomerOrder((long) 6,"huanyu2", "xiaozhang", "xiaoli", 9);
    		customerOrders.add(customerOrder);
    		customerOrders.add(customerOrder1);
    		customerOrders.add(customerOrder2);
    		customerOrders.add(customerOrder3);
    		customerOrders.add(customerOrder4);
    		customerOrders.add(customerOrder5);
    		System.err.println(customerOrders.size());
    
    		List list = new ArrayList<>();
    		List potentialCustomerOrdersl=new ArrayList<>();
    		potentialCustomerOrdersl.addAll(customerOrders);
    		for (CustomerOrder c : customerOrders) {
    			String[] str = new String[3];
    			str[0] = c.getAccountNumber();
    			str[1] = c.getCustomerName();
    			str[2] = c.getBuyerAddress();
    			potentialCustomerOrdersl = (List) map.get(str);
    			map.put(str, list);
    		}
    
    		List customerOrderEntryList = new ArrayList();
    		if (map.size() > 0) {
    			customerOrderEntryList.addAll(map.entrySet());
    			for (Iterator iter = customerOrderEntryList.iterator(); iter.hasNext();) {
    				Map.Entry customerOrderEntry = (Map.Entry) iter.next();
    				List potentialCustomerOrders = (List) customerOrderEntry.getValue();
    				Collections.sort(potentialCustomerOrders, new Comparator() {
    
    					public int compare(Object obj1, Object obj2) {
    						CustomerOrder customerOrder1 = (CustomerOrder) obj1;
    						CustomerOrder customerOrder2 = (CustomerOrder) obj2;
    						return customerOrder1.getCustomerOrderno().compareTo(customerOrder2.getCustomerOrderno());
    					}
    
    				});
    			}
    		}
    		
    		
    		
    
    		Collections.sort(customerOrderEntryList, new Comparator() {
    			public int compare(Object obj1, Object obj2) {
    				Map.Entry entry1 = (Map.Entry) obj1;
    				Map.Entry entry2 = (Map.Entry) obj2;
    				String[] potentialCustomerOrderInfo1 = (String[]) entry1.getKey();
    				String accountNumber1 = potentialCustomerOrderInfo1[0];
    				String customerName1 = potentialCustomerOrderInfo1[1];
    				String buyerAddress1 = potentialCustomerOrderInfo1[2];
    				String[] potentialCustomerOrderInfo2 = (String[]) entry2.getKey();
    				String accountNumber2 = potentialCustomerOrderInfo2[0];
    				String customerName2 = potentialCustomerOrderInfo2[1];
    				String buyerAddress2 = potentialCustomerOrderInfo2[2];
    				if (accountNumber1.compareTo(accountNumber2) > 0) {
    					return 1;
    				}
    				if (accountNumber1.compareTo(accountNumber2) < 0) {
    					return -1;
    				}
    				if (null != customerName1 && null != customerName2) {
    					if (customerName1.compareTo(customerName2) > 0) {
    						return 1;
    					}
    					if (customerName1.compareTo(customerName2) < 0) {
    						return -1;
    					}
    				}
    				if (buyerAddress1.compareTo(buyerAddress2) > 0) {
    					return 1;
    				}
    				if (buyerAddress1.compareTo(buyerAddress2) < 0) {
    					return -1;
    				}
    				return 0;
    			}
    
    		});
    		System.err.println(customerOrderEntryList.size());
    	}
    
    }
    
    class CustomerOrder {
    	private Long customerOrderno;
    	private String accountNumber;
    	private String customerName;
    	private String buyerAddress;
    	private int age;
    
    	public CustomerOrder(Long customerOrderno, String accountNumber, String customerName, String buyerAddress,
    			int age) {
    		this.customerOrderno = customerOrderno;
    		this.accountNumber = accountNumber;
    		this.customerName = customerName;
    		this.buyerAddress = buyerAddress;
    		this.age = age;
    	}
    
    	public String getAccountNumber() {
    		return accountNumber;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public void setAccountNumber(String accountNumber) {
    		this.accountNumber = accountNumber;
    	}
    
    	public String getCustomerName() {
    		return customerName;
    	}
    
    	public void setCustomerName(String customerName) {
    		this.customerName = customerName;
    	}
    
    	public String getBuyerAddress() {
    		return buyerAddress;
    	}
    
    	public void setBuyerAddress(String buyerAddress) {
    		this.buyerAddress = buyerAddress;
    	}
    
    	public Long getCustomerOrderno() {
    		return customerOrderno;
    	}
    
    	public void setCustomerOrderno(Long customerOrderno) {
    		this.customerOrderno = customerOrderno;
    	}
    
    }
    //返回值是数组的形式,并且如果对象一样就会返回一样的值

    [[Ljava.lang.String;@1e18a62b=[com.swfarm.biz.chain.bo.CustomerOrder@2da9f009[order id: =4028802d5f9e5000015f9e5ac184001c], com.swfarm.biz.chain.bo.CustomerOrder@6fcdf64a[order id: =4028802d5f9e5000015f9e5ac565001e]],


    [Ljava.lang.String;@3d159e0c=[com.swfarm.biz.chain.bo.CustomerOrder@3ae4ad9e[order id: =402882445fc291fb015fc2e78bf900d2], com.swfarm.biz.chain.bo.CustomerOrder@7b427e46[order id: =402882445fe289dd015fe2a98c32001d]],

    [Ljava.lang.String;@6445607c=[com.swfarm.biz.chain.bo.CustomerOrder@4a7064e2[order id: =40288244615516b301615518c5e2002f], com.swfarm.biz.chain.bo.CustomerOrder@18ef579c[order id: =40288244615516b301615518c8460033]],

    [Ljava.lang.String;@6c1a052=[com.swfarm.biz.chain.bo.CustomerOrder@960cf8e[order id: =4028824460811dc001608126edd80002], com.swfarm.biz.chain.bo.CustomerOrder@32da7871[order id: =402882446081697d0160816c23ca0002], com.swfarm.biz.chain.bo.CustomerOrder@25195ccc[order id: =402882446081697d016081761b560006]]]

  • 相关阅读:
    DNN学习笔记代码学习:LogDetailInfo 荣
    DNN学习笔记代码学习:BasePortalException 荣
    DNN学习笔记代码学习:LogInfo 荣
    DNN学习笔记代码学习:ExceptionModule 荣
    DNN学习笔记代码学习:LoggingProvider 荣
    DNN学习笔记代码学习:LogProperties 荣
    DNN学习笔记代码学习:LogController 荣
    DNN学习笔记代码学习:ExceptionLogController 荣
    DNN学习笔记代码学习:LogInfoArray 荣
    DNN学习笔记代码学习:CBO 荣
  • 原文地址:https://www.cnblogs.com/liushisaonian/p/8588422.html
Copyright © 2011-2022 走看看