I have a PriorityQueue of Objects and I need to sort it based on float values.How do I create a comparator for it?

huangapple go评论71阅读模式
英文:

I have a PriorityQueue of Objects and I need to sort it based on float values.How do I create a comparator for it?

问题

我已经为优先队列创建了一个比较器。

class CompareBySalary implements Comparator<Employee> {
    @Override
    public int compare(Employee e1, Employee e2) {
        return e1.salary < e2.salary ? -1 : e1.salary > e2.salary ? 1 : 0;
    }
}

class Employee {
    String name;
    float salary;

    Employee(String name, float salary) {
        this.name = name;
        this.salary = salary;
    }
}

public class TryCode {

    public static void main(String args[]) {

        Employee e1 = new Employee("C", 10000);
        Employee e2 = new Employee("A", 5000.45f);
        Employee e3 = new Employee("D", 15000);
        Employee e4 = new Employee("B", 5000.67f);
        Queue<Employee> q = new PriorityQueue(new CompareBySalary());
        q.offer(e1);
        q.offer(e2);
        q.offer(e3);
        q.offer(e4);
        for (Employee e : q)
            System.out.println(e.name);

    }

}

这会输出:A B D C

为什么它没有正确排序?
有什么我漏掉的东西吗?
附注:我已经尝试使用Float.compare(),它也会给出相同的输出。

英文:

I have already created a comparator for the priority queue.

class CompareBySalary implements Comparator&lt;Employee&gt;{
    @Override
    public int compare(Employee e1,Employee e2){
        return e1.salary&lt;e2.salary ? -1 : e1.salary&gt;e2.salary ? 1 : 0;
        
    }
}

class Employee{
    String name;
    float salary;
    Employee(String name,float salary){
        this.name=name;
        this.salary=salary;
    }
}

 public class TryCode{
     
     public static void main(String args[])
     {
        
         Employee e1=new Employee(&quot;C&quot;,10000);
         Employee e2=new Employee(&quot;A&quot;,5000.45f);
         Employee e3=new Employee(&quot;D&quot;,15000);
         Employee e4=new Employee(&quot;B&quot;,5000.67f);
         Queue&lt;Employee&gt; q=new PriorityQueue(new CompareBySalary());
         q.offer(e1);
         q.offer(e2);
         q.offer(e3);
         q.offer(e4);
         for(Employee e:q)
             System.out.println(e.name);
            
     }
     
     
 }

This gives the output:A B D C
Why is it not sorting correctly?
Is it something I am missing?
P.S. I have already tried with Float.compare() it gives the same output.

答案1

得分: 0

  1. 更改你的领域模型如下:
class Employee implements Comparable<Employee> {
    private String name;
    private float salary;

    Employee(String name, float salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public float getSalary() {
        return salary;
    }

    @Override
    public int compareTo(Employee o) {
        if (this.getSalary() > o.getSalary()) {
            return 1;
        } else if (this.getSalary() < o.getSalary()) {
            return -1;
        } else {
            return 0;
        }
    }
}
  1. 移除 CompareBySalary(你将不再需要它)

  2. 创建你的 PriorityQueue 如下:

PriorityQueue<Employee> q = new PriorityQueue<>(Comparator.comparingDouble(Employee::getSalary));
英文:
  1. Change your domain model as follows:

     class Employee implements Comparable&lt;Employee&gt; {
         private String name;
         private float salary;
    
         Employee(String name, float salary) {
             this.name = name;
             this.salary = salary;
         }
    
         public String getName() {
             return name;
         }
    
         public float getSalary() {
             return salary;
         }
    
         @Override
         public int compareTo(Employee o) {
             if (o1.getSalary()&gt;o2.getSalary()) {
                 return 1;
             } else if (o1.getSalary()&lt;o2.getSalary()) {
                 return -1;
             } else {
                 return 0;
             }
         }
     }
    
  2. Remove CompareBySalary (you won't need it)

  3. Create your PriorityQueue like this:

    PriorityQueue&lt;Employee&gt; q = new PriorityQueue&lt;&gt;(Comparator.comparingDouble(Employee::getSalary));
    

huangapple
  • 本文由 发表于 2020年8月10日 02:18:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63329797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定