Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Priority Queue with two Priorities Python

I'm searching for a kind of priority queue which allows me to give two priorites. I want that it just check for the first value then for the second one Here is some Code

import Queue

class Job(object):
    def __init__(self, fpriority, spriority, description, iata , hops, cost):
        self.fpriority = fpriority
        self.spriority = spriority

q = Queue.PriorityQueue()

q.put(Job(2, 5, 'Mid-level job'))
q.put(Job(2, 20, 'Low-level job'))
q.put(Job(1, 20, 'Important job'))

now i want the following order of the elements

Important job
Mid_level job
Low_level job

how can i create such an order with one queue?

like image 239
Sven Bamberger Avatar asked Dec 26 '22 11:12

Sven Bamberger


1 Answers

Just use the (fpriority, spriority) tuple as the priority. That'll do the sorting you want (compare first, then second to break ties).

like image 120
NPE Avatar answered Jan 03 '23 02:01

NPE