首先定义一个需要排序类 这里实现 自定义类 首个属性降序 其他升序
这个自定义类 不用实现什么接口 我这里是另做他用
package cn.tukk.sparkproject.spark.session;
import scala.Serializable;
import scala.math.Ordered;
public class MyCategorySortKey implements Ordered<MyCategorySortKey> , Serializable {
private static final long serialVersionUID = 197033426188166691L;
private int clickCount;
private int orderCount;
private int payCount;
public MyCategorySortKey(int clickCount, int orderCount, int payCount) {
this.clickCount = clickCount;
this.orderCount = orderCount;
this.payCount = payCount;
}
@Override
public int compare(MyCategorySortKey that) {
if(clickCount-that.getClickCount()!=0){
return (int) (clickCount-that.getClickCount());
}else if(orderCount-that.getOrderCount()!=0){
return (int) (clickCount-that.getOrderCount());
}else if(payCount-that.getPayCount()!=0){
return (int) (payCount-that.getPayCount());
}
return 0;
}
@Override
public boolean $greater(MyCategorySortKey other) {
if(clickCount > other.getClickCount()) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount > other.getOrderCount()) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount == other.getOrderCount() &&
payCount > other.getPayCount()) {
return true;
}
return false;
}
@Override
public boolean $greater$eq(MyCategorySortKey other) {
if($greater(other)) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount == other.getOrderCount() &&
payCount == other.getPayCount()) {
return true;
}
return false;
}
@Override
public boolean $less(MyCategorySortKey other) {
if(clickCount < other.getClickCount()) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount < other.getOrderCount()) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount == other.getOrderCount() &&
payCount < other.getPayCount()) {
return true;
}
return false;
}
@Override
public boolean $less$eq(MyCategorySortKey other) {
if($less(other)) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount == other.getOrderCount() &&
payCount == other.getPayCount()) {
return true;
}
return false;
}
@Override
public int compareTo(MyCategorySortKey that) {
if(clickCount-that.getClickCount()!=0){
return (int) (clickCount-that.getClickCount());
}else if(orderCount-that.getOrderCount()!=0){
return (int) (clickCount-that.getOrderCount());
}else if(payCount-that.getPayCount()!=0){
return (int) (payCount-that.getPayCount());
}
return 0;
}
public int getClickCount() {
return clickCount;
}
public void setClickCount(int clickCount) {
this.clickCount = clickCount;
}
public int getOrderCount() {
return orderCount;
}
public void setOrderCount(int orderCount) {
this.orderCount = orderCount;
}
public int getPayCount() {
return payCount;
}
public void setPayCount(int payCount) {
this.payCount = payCount;
}
@Override
public String toString() {
return "MyCategorySortKey{" +
"clickCount=" + clickCount +
", orderCount=" + orderCount +
", payCount=" + payCount +
'}';
}
}
其次编写排序测试
package cn.tukk.sparkproject.test.testOrder;
import cn.tukk.sparkproject.spark.session.MyCategorySortKey;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*
*/
public class Myorder {
public static void main(String[] args) {
MyCategorySortKey my1=new MyCategorySortKey(12,15,20);
MyCategorySortKey my2=new MyCategorySortKey(15,15,20);
MyCategorySortKey my3=new MyCategorySortKey(12,15,16);
MyCategorySortKey my4=new MyCategorySortKey(12,17,16);
List<MyCategorySortKey> list=new ArrayList<MyCategorySortKey>();
list.add(my1);
list.add(my2);
list.add(my3);
list.add(my4);
// 按点击数倒序
Collections.sort(list, new Comparator<MyCategorySortKey>() {
public int compare(MyCategorySortKey arg0, MyCategorySortKey arg1) {
if(arg0.getClickCount()-arg1.getClickCount()!=0){
if(arg0.getClickCount()-arg1.getClickCount()>0){//倒序
return -1;
}if(arg0.getClickCount()-arg1.getClickCount()<0){
return 1;
}else {
return 0;
}
}else if(arg0.getOrderCount()-arg1.getOrderCount()!=0){
return (int) (arg0.getOrderCount()-arg1.getOrderCount());
}else if(arg0.getPayCount()-arg1.getPayCount()!=0){
return (int) (arg0.getPayCount()-arg1.getPayCount());
}
return 0;
}
});
for (MyCategorySortKey my:list
) {
System.out.println(my);
}
}
}