questioncomparator.java

来自「一个在线学习系统的服务端SERVLET程序」· Java 代码 · 共 49 行

JAVA
49
字号
package eols.logic.test;
import java.util.Comparator;
import eols.bean.test.Question;
public class QuestionComparator implements Comparator {

    /** The sorting order of the hardNos */
    private boolean asc = false;

    /** Constructors */
    public QuestionComparator() {
        this(false);
    }
    public QuestionComparator(boolean asc) {
        this.asc = asc;
    }

    /** Set the sorting order of the questions */
    public void setAsc(boolean asc) {
        this.asc = asc;
    }

    /** Do not need to compare the equality with other comparators */
    public boolean equals(Object obj){
        return false;
    }

    /**
     * Comparator the question, based on the <code>hardNo</code> of each question
     *
     * @param o1 The first question to compare
     * @param o2 The second question to compare
     * @return Whether the hardNo of the first question is larger than the second one
     */
    public int compare(Object o1, Object o2){
        if (o1 == null)
            return -1;
        if (o2 == null)
            return 1;
        Question q1 = (Question)o1;
        Question q2 = (Question)o2;
        if (!asc) {
            return q1.getHardNo() > q2.getHardNo() ? 1 :
                q1.getHardNo() == q2.getHardNo() ? 0 : -1;
        } else {
            return q1.getHardNo() > q2.getHardNo() ? -1 :
                q1.getHardNo() == q2.getHardNo() ? 0 : 1;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?