012_Comparable和Comparator实例

1. 自定义SortUtil类

import java.util.Comparator;
import java.util.List;

public class SortUtil {
	/**
	 * List容器泛型比较器, 升序排序
	 * @param array
	 */
	public static <T extends Comparable<T>> void sort(List<T> list) {
		sort(list, null);
	}
	
	/**
	 * List容器泛型比较器, 带业务排序功能, 升序排序
	 * @param array
	 */
	@SuppressWarnings({ "unchecked" })
	public static <T> void sort(List<T> list, Comparator<T> c){
		Object[] array = list.toArray();
		int length = array.length;
		boolean sorted;
		// 趟数
		for(int i = 0; i < length - 1; i++) {
			sorted = true;
			// 次数
			for(int j = 0; j < length - i - 1; j++) {
				if((c != null && ((Comparator<Object>)c).compare(array[j], array[j + 1]) > 0) || (c == null && ((Comparable<Object>)array[j]).compareTo(array[j + 1]) > 0)) {
					Object temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
					sorted = false;
				}
			}
			
			if(sorted) { // 减少趟数
				break;
			}
		}
		
		for(int i = 0; i < array.length; i++) {
			list.set(i, (T)array[i]);
		}
	}
	
	/**
	 * 数组泛型比较器, 升序排序
	 * @param array
	 */
	public static <T extends Comparable<T>> void sort(T[] array) {
		sort(array, null);
	}
	
	/**
	 * 数组泛型比较器, 带业务排序功能, 升序排序
	 * @param array
	 */
	@SuppressWarnings("unchecked")
	public static <T> void sort(T[] array, Comparator<T> c) {
		int length = array.length;
		boolean sorted;
		// 趟数
		for(int i = 0; i < length - 1; i++) {
			sorted = true;
			// 次数
			for(int j = 0 ; j < length - i - 1; j++) {
				if((c != null && (c.compare(array[j], array[j + 1]) > 0)) || (c == null && ((Comparable<T>)array[j]).compareTo(array[j + 1]) > 0)) {
					T temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
					sorted = false;
				}
			}
			
			if(sorted) { // 减少趟数
				break;
			}
		}
	}
	
}

2. 使用SortUtil类

import java.util.Comparator;
import java.util.Date;

public class UseMySortUtil {
	public static void main(String[] args) {
		
	}
}

/**
 * 实现Comparator接口的,新闻条目安时间排序的业务类 
 */
class NewsTime implements Comparator<NewItem>{
	public int compare(NewItem o1, NewItem o2) {
		return o2.time.compareTo(o1.time);
	}
}

/**
 * 实现Comparable接口的新闻类
 */
class NewItem implements Comparable<NewItem>{
	public String title;
	public int hits;
	public Date time;
	
	public NewItem(String title, int hits, Date time) {
		this.title = title;
		this.hits = hits;
		this.time = time;
	}

	public int compareTo(NewItem o) {
		int result = 0;
		// 时间倒序
		if((result = o.time.compareTo(time)) == 0) {
			// 点击次数倒序
			if((result = (o.hits - hits)) ==0) {
				// 标题正序
				result = title.compareTo(o.title);
			}
		}
		
		return result;
	}
	
	public String toString() {
		return "[title = " + title + " hits = " + hits + " time = " + time + "]";
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值