Documentation only shows deriving from IComparer and then specifying the derived class as the comparator in the constructor of PriorityQueue, but with multiple priority queues, it creates too many extra classes.
Is it possible to just use Lambda comparator with PriorityQueue class in C#?
While the other answer is good, the quick one-liner solution as mentioned in comments by other users is:
//Descending Sort, Integer
var queue = new PriorityQueue<int, int>(Comparer<int>.Create((x, y) => y - x));
//Ascending Sort, Object
var queue = new PriorityQueue<ObjectA, ObjectB>(Comparer<ObjectB>.Create((x, y) => x.Something.CompareTo(y.Something));
Note that second element i.e. priority, can either be an object or primitive data type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With