java实现对象比较,可以实现java.lang.Comparable或java.util.Comparator接口
- //Product.java
- import java.util.Date;
- //public class Product implements Comparable {//类内比较
- public class Product{
- private String name;
- private Date date;
- private int price;
- public int getPrice() {
- return price;
- }
- public void setPrice(int price) {
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- //用于类内比较
- // @Override//覆写compareTo(Object o)方法
- // public int compareTo(Product o) {
- // return -this.date.compareTo(o.date);
- // }
- }
- //ProductComparable.java
- import java.util.Date;
- import java.util.Comparator;
- public class ProductComparable implements Comparator<Product> {
- // 对象的排序方式[升、降]
- public static boolean sortASC = true;
- // 对象的排序属性
- public static boolean sortByName = false;
- public static boolean sortByDate = false;
- public static boolean sortByPrice = false;
- @Override
- public int compare(Product pro1, Product pro2) {
- int result = 0;
- if(sortASC){
- if(sortByName){
- String pro1name = pro1.getName();
- String pro2name = pro2.getName();
- result = pro1name.compareTo(pro2name);
- }else if(sortByDate){
- Date pro1Date = pro1.getDate();
- Date pro2Date = pro2.getDate();
- result = pro1Date.compareTo(pro2Date);
- }else if(sortByPrice){
- Integer pro1Price = pro1.getPrice();
- Integer pro2Price = pro2.getPrice();
- result = pro1Price.compareTo(pro2Price);
- }
- }else{
- if(sortByName){
- String pro1name = pro1.getName();
- String pro2name = pro2.getName();
- result = -pro1name.compareTo(pro2name);
- }else if(sortByDate){
- Date pro1Date = pro1.getDate();
- Date pro2Date = pro2.getDate();
- result = -pro1Date.compareTo(pro2Date);
- }else if(sortByPrice){
- Integer pro1Price = pro1.getPrice();
- Integer pro2Price = pro2.getPrice();
- result = -pro1Price.compareTo(pro2Price);
- }
- }
- return result;
- }
- }
- //测试代码
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Locale;
- public class ProductSort {
- public static void main(String[] args) throws ParseException {
- Product pro1 = new Product();
- pro1.setName("pro1");
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-MM",Locale.CHINA);
- pro1.setDate(sdf.parse("2009-03-23"));
- pro1.setPrice(89);
- Product pro2 = new Product();
- pro2.setName("proc2");
- pro2.setDate(sdf.parse("2009-02-23"));
- pro2.setPrice(45);
- Product pro3 = new Product();
- pro3.setName("proc3");
- pro3.setDate(sdf.parse("2009-01-23"));
- pro3.setPrice(83);
- Product pro4 = new Product();
- pro4.setName("proc4");
- pro4.setDate(sdf.parse("2009-01-23"));
- pro4.setPrice(800);
- List<Product> productList = new ArrayList<Product>();
- productList.add(pro1);
- productList.add(pro3);
- productList.add(pro2);
- productList.add(pro4);
- System.out.println("排序前————————————————————");
- for(Product p: productList){
- System.out.println(p.getName()+"----"+p.getPrice()+"----"+p.getDate());
- }
- ProductComparable sort = new ProductComparable();
- ProductComparable.sortASC = false;//降序
- ProductComparable.sortByPrice=true; //设置排序属性生效
- Collections.sort(productList,sort);
- System.out.println("排序后————————————————————");
- for(Iterator<Product> iter = productList.iterator(); iter.hasNext();){
- Product p = (Product)iter.next();
- System.out.println(p.getName()+"----"+p.getPrice()+"---"+p.getDate());
- }
- }
- }
推荐使用Comparator实现排序
因为 你用一个第三方的类,但他没实现Comparable接口,而你又不能改他代码;另外,类一旦写好后是不允许修改的,但可以扩展,所以只能用Comparator接口