版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
一、想到排序大家肯定会想到什么冒泡排序这些基本算法,但是这个用起来不是那么的方便简单,今天给大家介绍一个java类,Collections。这个一个非常使用的数据结构。
二、在Collections中,有Collections.sort方法:看源码:
public static <T> void sort(List<T> list, Comparator<? super T> c) {
// BEGIN Android-changed: Compat behavior for apps targeting APIs <= 25.
// list.sort(c);
int targetSdkVersion = VMRuntime.getRuntime().getTargetSdkVersion();
if (targetSdkVersion > 25) {
list.sort(c);
} else {
// Compatibility behavior for API <= 25. http://b/33482884
if (list.getClass() == ArrayList.class) {
Arrays.sort((T[]) ((ArrayList) list).elementData, 0, list.size(), c);
return;
}
Object[] a = list.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<T> i = list.listIterator();
for (int j = 0; j < a.length; j++) {
i.next();
i.set((T) a[j]);
}
}
// END Android-changed: Compat behavior for apps targeting APIs <= 25.
}
在方法中需要传递两个参数:list,和一个比较器Comparator,在使用过程中,new Comparator时会实现compare方法,具体使用方法如下:
1、现在对一个对象进行排序,bean类为:
public class StockMarket {
private List<RowsBean> rows;
public List<RowsBean> getRows() {
return rows;
}
public void setRows(List<RowsBean> rows) {
this.rows = rows;
}
public static class RowsBean {
/**
* gpcode : 600050
* lastprice : 5.42
* name : 涓浗鑱旈€�
* newprice : 5.34
* rate : -1.4760147601476028
*/
private String gpcode;
private double lastprice;
private String name;
private double newprice;
private double rate;
private int stoptrade;
public int getStoptrade() {
return stoptrade;
}
public void setStoptrade(int stoptrade) {
this.stoptrade = stoptrade;
}
public String getGpcode() {
return gpcode;
}
public void setGpcode(String gpcode) {
this.gpcode = gpcode;
}
public double getLastprice() {
return lastprice;
}
public void setLastprice(double lastprice) {
this.lastprice = lastprice;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getNewprice() {
return newprice;
}
public void setNewprice(double newprice) {
this.newprice = newprice;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
}
2、我们需要对该bean类中的newprice进行,升序和倒序排序,先是倒序排序,使用的o2.xx-o1.xx,这样获得的List就是倒序排序后的列表了:
public List<StockMarket.RowsBean> getLastPriceDescList(List<StockMarket.RowsBean> list) {
Collections.sort(list, new Comparator<StockMarket.RowsBean>() {
@Override
public int compare(StockMarket.RowsBean o1, StockMarket.RowsBean o2) {
return Double.valueOf(o2.getNewprice()).compareTo(Double.valueOf(o1.getNewprice()));
}
});
return list;
}
3、升序排序,使用o1.xxx-o2.xxx排序获得升序列表:
public List<StockMarket.RowsBean> getLastPriceASCList(List<StockMarket.RowsBean> list) {
Collections.sort(list, new Comparator<StockMarket.RowsBean>() {
@Override
public int compare(StockMarket.RowsBean o1, StockMarket.RowsBean o2) {
return Double.valueOf(o1.getNewprice()).compareTo(Double.valueOf(o2.getNewprice()));
}
});
return list;
}
4、这里要注意的是,假如你要排序的数据类型是double的话,就像我这个例子中的,那么在Comparator中的返回值是int,如果你直接把你的double强制转换成int,然后将这返回这两个int值的差的话,那么,你的排序将会出问题,你的double被强转了,此时就应该用 Double.valueOf()来转换int,然后用compareTo进行比较,这样就不会出问题了,如果你本身就是int数据类型,直接作差比较就好了。en,就酱紫!