0

This question Easy idiomatic way to define Ordering for a simple case class looks similar but not same. This question is more related to priority queue.

import scala.collection.mutable.PriorityQueue

  case class Task(prio: Int, text: String, jobPriority:Int)
    extends Ordered[Task] {
    def compare(that: Task) = that.prio compare this.prio
   // def compare(x: Task, y: Task) = y.prio compare this.prio //&&  y.jobPriority compare this.jobPriority

  }

  //test
  var q: mutable.PriorityQueue[Task] = PriorityQueue[Task]() ++ Seq(Task(3, "Clear drains",1), Task(4, "Feed cat",2),
    Task(5, "Make tea",9), Task(1, "Solve RC tasks",5), Task(2, "Tax return",5))
  while (q.nonEmpty) println(q dequeue)

is giving me this output :

Task(1,Solve RC tasks,5)
Task(2,Tax return,5)
Task(3,Clear drains,1)
Task(4,Feed cat,2)
Task(5,Make tea,9)

now I want to consider jobPriority as well while doing comparison. just like custom java comparator which compares multiple attributes.

Is there any way in scala to achieve this ?

user3190018
  • 890
  • 13
  • 26
  • 2
    Does this answer your question? [Easy idiomatic way to define Ordering for a simple case class](https://stackoverflow.com/questions/19345030/easy-idiomatic-way-to-define-ordering-for-a-simple-case-class) – jrook Oct 30 '19 at 22:39
  • It is still a duplicate. Based on the answer you have accepted, the linked question is an exact answer to your question. – jrook Nov 01 '19 at 18:15

2 Answers2

2

You can do something like this:

case class Task(prio: Int, text: String, jobPriority:Int)
    extends Ordered[Task] {
    def prioCompare(that: Task) = that.prio compare this.prio
    def jobPriorityCompare(that: Task) = that.jobPriority compare this.jobPriority
    def compare(that: Task) = {
      val prioC = prioCompare(that)

      if(prioC == 0) jobPriorityCompare(that) else prioC
    }

  }

  //test
  var q: mutable.PriorityQueue[Task] = PriorityQueue[Task]() ++ Seq(Task(3, "Clear drains",6), Task(4, "Feed cat",2),
    Task(5, "Make tea",9), Task(1, "Solve RC tasks",5), Task(3, "Tax return",5))
  while (q.nonEmpty) println(q dequeue)

/* Output
Task(1,Solve RC tasks,5)
Task(3,Tax return,5)
Task(3,Clear drains,6)
Task(4,Feed cat,2)
Task(5,Make tea,9)
*/

Pedro Correia Luís
  • 1,085
  • 6
  • 16
1

comparing with tuple :

import scala.collection.mutable.PriorityQueue

  case class Task(prio: Int, text: String, jobPrio: Int)
    extends Ordered[Task] {

    import scala.math.Ordered.orderingToOrdered

    def compare(that: Task) = (that.prio, that.jobPrio) compare(this.prio, this.jobPrio)

  }

  //test
  var q: mutable.PriorityQueue[Task] = PriorityQueue[Task]() ++ Seq(Task(3, "Clear drains", 1), Task(4, "Feed cat", 2),
    Task(5, "Make tea", 9), Task(1, "Solve RC tasks", 5), Task(2, "Tax return", 5))
  while (q.nonEmpty) println(q dequeue)

Result :

Task(1,Solve RC tasks,5)
Task(2,Tax return,5)
Task(3,Clear drains,1)
Task(4,Feed cat,2)
Task(5,Make tea,9)
Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121