以下内容整理自互联网。最后有可导入eclipse的源码附件。@雪夜丶辉煌 Comparator 强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collections.sort或Arrays.sort。接口方法:[Java] 纯文本查看 复制代码 int compare(T o1, T o2); 强行对实现它的每个类的对象进行整体排序,实现此接口的对象列表(和数组)可以通过Collections.sort或Arrays.sort进行自动排序。
接口方法:[Java] 纯文本查看 复制代码 public int compareTo(T o); 1.Comparator比较的例子Product.java[Asm] 纯文本查看 复制代码 package object_sort;
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[Asm] 纯文本查看 复制代码 package object_sort;
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;
}
}
ProductSort.java[Java] 纯文本查看 复制代码 package object_sort;
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());
// }
for(Product p: productList){
System.out.println(p.getName()+"----"+p.getPrice()+"----"+p.getDate());
}
}
}
2.Comparable例子Employee.java[Java] 纯文本查看 复制代码 import java.util.Random;
/*
* 因为要实现对Employee对象的排序,所以在Employee类中要实现Comparable接口,
* 也就是要实现comepareTo()方法
*/
public class Employee implements Comparable<Employee> {
private int id;
private String name;
private double salary;
public Employee(String n, double s)
{
name = n;
salary = s;
Random ID = new Random();
id = ID.nextInt( 10000000 );
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public void raiseSalary( double byPercent)
{
double raise = salary *byPercent/ 100 ;
salary+=raise;
}
public int compareTo(Employee other)
{
if (id<other.id) //这里比较的是什么 sort方法实现的就是按照此比较的东西从小到大排列
return - 1 ;
if (id>other.id)
return 1 ;
return 0 ;
}
}
EmployeeSortTest.java[Java] 纯文本查看 复制代码 import java.util.Arrays;
public class EmployeeSortTest
{
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee[] staff = new Employee[ 3 ];
staff[ 0 ] = new Employee( "harry Hacker" , 35000 );
staff[ 1 ] = new Employee( "carl cracke" , 75000 );
staff[ 2 ] = new Employee( "tony Tester" , 38000 );
Arrays.sort(staff); //sort方法可以实现对对象数组排序,但是必须实现 Comparable接口
/*Comparable接口原型为:
* public interface Comparable<T>
* {
* int compareTo(T other);//接口的中方法自动属于public方法
* }
*/
for (Employee e: staff)
System.out.println( "id=" +e.getId()+ " name=" +e.getName()+
".salary=" +e.getSalary());
}
}
Comparator和Comparable实现排序的例子源码.zip
(12.13 KB, 下载次数: 0)
|