很容易嘛
List ascList = new ArrayList(list);//复制一份,如果不这样,则会修改原来的list顺序的
Collections.sort(ascList,new Comparator() {
public int compare(Object o1, Object o2) {
IntNum i1 = (IntNum)o1;
IntNum i2 = (IntNum)o2;
return i1.getNum() - i2.getNum();
}
});List desList = new ArrayList(list);//复制一份,如果不这样,则会修改原来的list顺序的
Collections.sort(desList ,new Comparator() {
public int compare(Object o1, Object o2) {
IntNum i1 = (IntNum)o1;
IntNum i2 = (IntNum)o2;
return i2.getNum() - i1.getNum();
}
});
List ascList = new ArrayList(list);//复制一份,如果不这样,则会修改原来的list顺序的
Collections.sort(ascList,new Comparator() {
public int compare(Object o1, Object o2) {
IntNum i1 = (IntNum)o1;
IntNum i2 = (IntNum)o2;
return i1.getNum() - i2.getNum();
}
});List desList = new ArrayList(list);//复制一份,如果不这样,则会修改原来的list顺序的
Collections.sort(desList ,new Comparator() {
public int compare(Object o1, Object o2) {
IntNum i1 = (IntNum)o1;
IntNum i2 = (IntNum)o2;
return i2.getNum() - i1.getNum();
}
});
解决方案 »
- 在线等 求高手 速来秒杀
- JAVA 面试题目 貌似有点难 高手赐教
- java新手的福音
- 在jdk1.3中有什么办法替换jdk1.4的replaceAll的方法?
- 请问eclipse中为什么在cmd中运行要快啊
- Synchronizer+MyEclipse创建hibernate.cfg.xml文件的时候,怎么老是这个错误,该怎么解决,急!!!
- 高价求高手开发一个QQ聊天室外挂程序
- oracle 9i的连接池,那位大大用过,请说明下调用的顺序!
- %%%%%% 高分请教, 在Java Application中如何打开Excel文件和打开IE窗口??????????????
- 提取文本框内容一定要加响应事件才行吗?
- j2SE是否是个成熟的产品?我已经出离得愤怒了!
- 在问一个简单问题,送分了
查询数据库得到一个ResultSet,每一个字段的值保存在一个Entity对象中,每一行数据由多个Entity组成,一行数据的多个Entity对象保存在一个LinkedList中。一个ResultSet由多行数据组成,所以一个ResultSet里的多个LinkedList对象保存在ArrayList中。排序的时候对ArrayList排序,可以指定按任意的字段,任意的升序或降序排序。贴代码就太多了吧?
private int num;
public IntNum(int num){ this.num=num;}
public int getNum(){return num;} public int compareTo(Object o) {
IntNum test = (IntNum ) o;
int diff = x - test.num;
return diff;
}
}List list=new ArrayList();
list.add(new IntNum(32));
list.add(new IntNum(13));
list.add(new IntNum(190));
list.add(new IntNum(74));
list.add(new IntNum(43));private void ascList(list) {
Collections.sort(list);
}
List ascList = ascList(list);降序的话
int diff = test.num - x;
但这样不能同时实现。我再想想
也可以自己手工写,不过效率一样
public class SortTest implements Comparable{
private int x;
private int y;
public SortTest(int x, int y) {
this.x = x;
this.y = y;
} public int compareTo(Object o) {
SortTest test = (SortTest) o;
int diff = x - test.x;
return diff;
} public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
List list = new ArrayList();
SortTest t1 = new SortTest(8,2);
SortTest t2 = new SortTest(-8,1);
SortTest t3 = new SortTest(5,1);
list.add(t1);
list.add(t2);
list.add(t3);
// Collections.sort(list);
Collections.reverse(list);
Iterator it = list.iterator();
while (it.hasNext()) {
SortTest tx = (SortTest) it.next();
System.out.println(tx.x + " " + tx.y);
}
}
}
为什么是
5 1
-8 1
8 2
//Entity.java
////////////////////
package com.tasia.capsulate;import java.math.BigDecimal;
import java.util.Date;/**
* 用于存储ResultSet记录中的一个字段的值。
* ResultSet中每一个字段的值使用"键-值"对应的方式进行存储,其中"键"为字段的列名(已转换为全部小写),"值"为数据库中存储的值。
*/
class Entity{
String key = null;
Object value = null; public Entity(String key, Object value){
this.key = key.toLowerCase();
this.value = value;
} /**
* 返回这个对象的"键",即它的列名
* @return String 返回这个对象的"键"
*/
public String getKey(){
return this.key;
} /**
* 返回这个字段的值,以Object对象的形式
* @return Object 值
*/
public Object getValue(){
return this.value;
} /**
* 将本Entity与另外一个对象比较。如果另外一个对象不是Entity,抛出ClassCastException;否则比较这两个
*Entity的Value,如果两个Value的运行时类型不同,抛出ClassCastException;否则比较它们的值:相等时返回0;
*本对象较小返回小于0的值;本对象较大时返回大于0的值。
*
* @param obj Object 要与之进行比较的对象。
* @return int 两个对象相等时返回0;本对象较小返回小于0的值;本对象较大时返回大于0的值。
*
* @throws ClassCastException 如果传进来的参数不是Entity的对象。
* @throws ClassCastException 如果传进来的对象的value的运行时类型与本对象的value的运行时类型不同。
*/
public int CompareTo(Object obj){
int rtnValue = 0;
if(obj instanceof Entity){
String key1 = ((Entity)obj).getKey();
Object value1 = ((Entity)obj).getValue(); if (this.value instanceof String) {
if( value1 instanceof String){
String s1 = (String) this.value;
String s2 = (String) value1;
rtnValue = s1.toLowerCase().compareTo(s2.toLowerCase());
}else{
throw new ClassCastException();
}
} if (this.value instanceof BigDecimal) {
if( value1 instanceof BigDecimal){
BigDecimal s1 = (BigDecimal) this.value;
BigDecimal s2 = (BigDecimal) value1;
rtnValue = new Long(s1.longValue()).compareTo(new Long(s2.longValue()));
}else{
throw new ClassCastException();
}
} if (this.value instanceof Date) {
if( value1 instanceof Date){
Date s1 = (Date) this.value;
Date s2 = (Date) value1;
rtnValue = s1.compareTo(s2);
}else{
throw new ClassCastException();
}
}
}else{
throw new ClassCastException();
} return rtnValue;
}
}
/////////////////////////////
//Comparator.java
/////////////////////////////
package com.tasia.capsulate;import java.util.LinkedList;/**
* 这个类创建一个比较器,指定按照某一列的升序或降序排列数据.
* <p>Title: Data.Comparator</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: Soutecit</p>
* @author Zhong Liangli
* @version 1.0
*/
public class Comparator implements java.util.Comparator{
private int column = 1; //按第几列排序
private boolean orderByAsc = true; //是否按升序排序 /**
* 按指定的列和指定的升降顺序排序
* @param columnIndex int 要依据它对数据进行排序的列的列号
* @param orderByAsc boolean 指定按升序或降序排序
*/
public Comparator(int columnIndex, boolean orderByAsc){
this.column = columnIndex;
this.orderByAsc = orderByAsc;
}
/**
* 比较两个行的大小,这个方法用于数据排序时。一个行的大小由指定的要对其进行排序的字段的大小来决定。
*这个方法先取得一行数据的指定的列的值,对该值进行比较,得到比较的值后再乘以升降序因子(升序时因子的值为1,
*降序值为-1)得到最后的结果.
* @param obj1 Object 要进行比较的第一个对象
* @param obj2 Object 要进行比较的第二个对象
* @return int 当第一个对象应排在前面时返回负数;相等时返回0;当第二个对象应排在前面时返回正数。
*/
public int compare(Object obj1, Object obj2){
int rtnValue = 0;
int factor = this.orderByAsc ? 1 : -1; LinkedList row1 = (LinkedList)obj1;
LinkedList row2 = (LinkedList)obj2; Entity field1 = (Entity)row1.get(this.column-1);
Entity field2 = (Entity)row2.get(this.column-1);
rtnValue = field1.CompareTo(field2); return factor * rtnValue;
}
}
package com.soutecit.capsulate;import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;/**
* 查询数据库后,用得到的ResultSet创建一个本类的对象。这个类把从数据库中查询得到的结果集中的数据封装起来,
*并实现了数据的存取等操作。
*
* @author Zhong Liangli
* @version 1.0
*/
public class Data {
private ArrayList list = null;
private ListIterator ite = null; /**
* 创建一个对象,并把ResultSet中的数据存储在ArrayList中.
* 如果在读取ResultSet的过程中发生异常,则返回null.
* @param rs ResultSet 提供数据的结果集
* @throws SQLException 如果在读取ResultSet的数据时它抛出异常
*/
public Data(ResultSet rs)throws SQLException{
if(rs == null)
return; list = new ArrayList();
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();
while (rs.next()) {
LinkedList row = new LinkedList();
for(int i = 1; i <= columnCount; i++){
Object obj = rs.getObject(i);
Entity e = new Entity(meta.getColumnName(i).toLowerCase(), obj);
row.addLast(e);
}
//System.out.println("column count = " + columnCount);
list.add(row);
} ite = list.listIterator();
} /**
* 按指定的列和指定的升降顺序排序
* @param columnIndex int 要依据它对数据进行排序的列的列号
* @param orderByAsc boolean 指定按升序或降序排序
*/
public void sort(int columnIndex, boolean orderByAsc){
java.util.Collections.sort(list, new Comparator(columnIndex, orderByAsc));
ite = list.listIterator();
}
}
* @(#) TestIntNumComparator.java Created on 2004-10-17 By James Fancy
*/import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;class IntNum { public int num; public IntNum(int num) {
this.num = num;
} public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
}
public String toString() {
return String.valueOf(num);
}
}class IntNumComparator implements Comparator {
protected boolean isAsc;
public IntNumComparator(boolean asc) {
isAsc = asc;
}
public IntNumComparator() {
this(true);
} public int compare(IntNum in1, IntNum in2) {
int r = in2.getNum() - in1.getNum();
if (isAsc) {
return r;
} else {
return -r;
}
} public int compare(Object obj1, Object obj2) {
try {
return compare((IntNum) obj1, (IntNum) obj2);
} catch (ClassCastException e) {
throw new IllegalArgumentException(e);
}
}}public class TestIntNumComparator {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new IntNum(32));
list.add(new IntNum(13));
list.add(new IntNum(190));
list.add(new IntNum(74));
list.add(new IntNum(43)); List ascList = ascList(list);
List desList = desList(list);
printList(ascList);
System.out.println("===========================");
printList(desList);
} private static void printList(List list) {
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} private static List desList(List list) {
list = new ArrayList(list);
Collections.sort(list, new IntNumComparator());
return list;
} private static List ascList(List list) {
list = new ArrayList(list);
Collections.sort(list, new IntNumComparator(false));
return list;
}}
/*
* @(#) TestIntNumComparator.java
* Created on 2004-10-17
* By James Fancy
*/import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;class IntNum { public int num; public IntNum(int num) {
this.num = num;
} public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
} public String toString() {
return String.valueOf(num);
}
}class IntNumComparator implements Comparator { protected boolean isAsc; public IntNumComparator(boolean asc) {
isAsc = asc;
} public IntNumComparator() {
this(true);
} public int compare(IntNum in1, IntNum in2) {
int r = in2.getNum() - in1.getNum();
if (isAsc) {
return r;
} else {
return -r;
}
} public int compare(Object obj1, Object obj2) {
try {
return compare((IntNum) obj1, (IntNum) obj2);
} catch (ClassCastException e) {
throw new IllegalArgumentException(e);
}
}}class ListSorter { Comparator ascComparator;
Comparator descComparator; public ListSorter(Comparator ascComparator, Comparator descComparator) {
this.ascComparator = ascComparator;
this.descComparator = descComparator;
} protected List cloneList(List list) {
Class c = list.getClass();
try {
List newList = (List) c.newInstance();
newList.addAll(list);
return newList;
} catch (InstantiationException e) {
return null;
} catch (IllegalAccessException e) {
return null;
} } public List ascList(List list) {
List newList = cloneList(list);
Collections.sort(newList, ascComparator);
return newList;
} public List descList(List list) {
List newList = cloneList(list);
Collections.sort(newList, descComparator);
return newList;
}
}public class TestIntNumComparator { public static void main(String[] args) {
List list = new ArrayList();
list.add(new IntNum(32));
list.add(new IntNum(13));
list.add(new IntNum(190));
list.add(new IntNum(74));
list.add(new IntNum(43)); ListSorter sorter = new ListSorter(new IntNumComparator(),
new IntNumComparator(false)); List ascList = sorter.ascList(list);
List desList = sorter.descList(list); printList(ascList);
System.out.println("===========================");
printList(desList);
} private static void printList(List list) {
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}}
1.如果要排序的对象实现了Comparable接口
那么升序: Collections.sort(list);
降序: Collections.sort(list,Collections.reverseOrder());
2.如果手动实现Comparator接口,最好定义一个通用类ReverseComparotor
public class ReverseComparotor implements Comparator,Serializable {
private Comparator comp;
public ReverseComparator(Comparator comp){
this.comp=comp;
}
public int compare(Object obj1,Object obj2){
int cmp=comp.compare(obj1,obj2);
return -(cmp|cmp>>>1);
}
}
升序的时候:Collections.sort(list,new MyComparator());
降序的时候:Collections.sort(list,new ReverseComparator(new MyComparator()));
说得对哦,要是真的要求高效率的算法,要根据数据的特点来。