Lexicographic Order in Java

后端 未结 4 1280
迷失自我
迷失自我 2021-02-19 15:21

How is the lexicographic order defined in Java especially in reference to special characters like !, . and so on?

An examplary order can be fou

4条回答
  •  遥遥无期
    2021-02-19 15:56

    Hope this helps!!

    Employee sorted based on the descending order of the score and if two different employee has same score, then we need to consider Employee name for sorting lexicographically.

    Employee class implementation: (Used Comparable interface for this case.)

    @Override
    public int compareTo(Object obj) {
        Employee emp = (Employee) obj;
    
        if(emp.getScore() > this.score) return 1;
        else if(emp.getScore() < this.score) return -1;
        else
            return emp.getEmpName().compareToIgnoreCase(this.empName) * -1;
    }
    

提交回复
热议问题