冒泡排序

private Object[] sort(ArrayList items, Comparator comparator) {
		Object[] itemArray = items.toArray();
		int len = itemArray.length;
		for (int i = 0; i < len - 1; i++) {
			for (int j = len - 1; j > i; j--) {
				if (comparator.compare(itemArray[j], itemArray[j-1]) < 0) {
					Object temp = itemArray[j];
					itemArray[j] = itemArray[j-1];
					itemArray[j-1] = temp;
				}
			}
		}
		return itemArray;
	}


class NewsBulletinComparator{
    @Override
			public int compare(Object o1, Object o2) {
				Date date1 = null;
				Date date2 = null;
				if (o1 instanceof News) {
					News news = (News) o1;
					date1 = news.getPubDate();
				} else if(o1 instanceof Bulletin) {
					Bulletin bulletin = (Bulletin) o1;
					date1 = bulletin.getPubDate();
				}
				if (o2 instanceof News) {
					News news = (News) o2;
					date2 = news.getPubDate();
				} else if(o2 instanceof Bulletin) {
					Bulletin bulletin = (Bulletin) o2;
					date2 = bulletin.getPubDate();
				}
				return date1.compareTo(date2);
			}
}

你可能感兴趣的:(冒泡排序)