排序测试

package test;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.Query;

import util.DateTime;
import util.SortUtil;

import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;

import data.table.log.LCharge;

public class Student implements Cloneable {
    private int age;
    private String name;
    private float mark;
    private double score;
    private boolean isonline;
    private Long time;
    private Object a;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return (Student) super.clone();
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getMark() {
        return mark;
    }

    public void setMark(float mark) {
        this.mark = mark;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public boolean isIsonline() {
        return isonline;
    }

    public void setIsonline(boolean isonline) {
        this.isonline = isonline;
    }
    
    
    

    public Long getTime() {
        return time;
    }

    public void setTime(long time) {
        a = time;
        this.time = time;
    }

    public static Comparator<Object> getComp() {
        return comp;
    }

    public static void setComp(Comparator<Object> comp) {
        Student.comp = comp;
    }

    @Override
    public String toString() {
        return "Student [age=" + age + ", name=" + name + ", mark=" + mark
                + ", score=" + score + ", isonline=" + isonline + "]";
    }

    public Student(String name, int age, float mark, double score,
            boolean isonline) {
        this.age = age;
        this.name = name;
        this.mark = mark;
        this.score = score;
        this.isonline = isonline;
    }

    public Student() {
    }


    public Map<Integer, Integer> mapFun() {
        Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
        mp.put(1, 1);
        return mp;
    }

    @SuppressWarnings("unchecked")
    public static <T, F> Map<T, F> getMap(Object... args) {
        Map<T, F> map = new HashMap<T, F>();
        for (int i = 0; i < args.length; i += 2) {
            map.put((T) args[i], (F) args[i + 1]);
        }
        return map;
    }

    private static Comparator<Object> comp = new Comparator<Object>() {
        public int compare(Object o1, Object o2) {
            int a1 = ((Student) o1).getAge();
            int a2 = ((Student) o2).getAge();
            if (a1 < a2) {
                return 1;
            } else {
                return 0;
            }
        }
    };

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Map<Long, Integer> sort(Map<Long, Integer> map) {
        List<Integer> list = new LinkedList(map.entrySet());
        Collections.sort(list, new Comparator() {
            public int compare(Object o1, Object o2) {
                int t1 = ((Map.Entry<Integer, Integer>) o1).getValue();
                int t2 = ((Map.Entry<Integer, Integer>) o2).getValue();
                return t1 - t2 > 0 ? -1 : 1;
            }
        });
        Map<Long, Integer> result = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry<Long, Integer> entry = (Map.Entry<Long, Integer>) it
                    .next();
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

    
    /**
     * @param args
     * @throws MongoException
     * @throws UnknownHostException
     */
    public static void main(String[] args) throws Exception {
        List<Object> st = new ArrayList<Object>();
        Student sd1 = new Student("chen1", 7, 7f, 1.3, true);
        Student sd2 = new Student("chen2", 4, 4f, 2.2, true);
        Student sd3 = new Student("chen3", 5, 5f, 3.2, true);
        Student sd4 = new Student("chen4", 3, 3f, 4.3, true);
        Student sd5 = new Student("chen5", 2, 2f, 5.3, true);
        Student sd6 = new Student("chen6", 7, 7f, 6.3, false);
        Student sd7 = new Student("chen7", 7, 6f, 6.3, false);
        Student sd8 = new Student("chen8", 8, 8f, 9.3, false);
        sd8.setTime(12l);
        st.add(sd1);
        st.add(sd2);
        st.add(sd3);
        st.add(sd4);
        st.add(sd5);
        st.add(sd6);
        st.add(sd7);
        st.add(sd8);
        SortUtil.sort(st, "isIsonline#-1", "getScore#1");
        for(Object o:st){
            System.out.println(o);
        }
    }
    
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值