question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could this library support customized function as comparator for priority property?

See original GitHub issue

Hey, when I use this in leetcode, the use case is when I want a complicated comparator rather than simply compare by one value:

const biddersQueue = new MaxPriorityQueue({ priority: (bid) => bid.value });
// something i want:
const biddersQueue = new MaxPriorityQueue({ priority: (a,b) =>{  // comparator function} } );

maybe can this library support a comparator function in constructor?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
emreozdincercommented, Jun 29, 2021

Hi @eyas-ranjous, I feel the same need as @jialihan.

One example is this question. Here, we are required to sort words by frequency, but if the word frequencies of two words are the same, then we need to sort them alphabetically.

For example, this is the initialization in Java:

PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>(
                 (a,b) -> a.getValue()==b.getValue() ? b.getKey().compareTo(a.getKey()) : a.getValue()-b.getValue()
        );

Here is a mocked-up implementation of the expected functionality in JS.

Basically, priority number of a single item isn’t enough to decide the result of the comparison - we need to be able to write a custom comparator.

Cheers

0reactions
eyas-ranjouscommented, Aug 8, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Priority Queue with a custom anonymous comparator
You can use a PriorityQueue based on an anonymous Comparator passed to the constructor: ... Furthermore, PriorityQueues support generic data types.
Read more >
Custom Comparator in Priority_queue in C++ STL
Comparator in the case of priority_queue decides the ordering of elements, which is basically in the competition among the elements, it decides ...
Read more >
PriorityBlockingQueue (Java Platform SE 7 )
If you need to enforce an ordering, you can define custom classes or comparators that use a secondary key to break ties in...
Read more >
Use the STL PRIORITY_QUEUE class with a custom type
This article provides a code sample that describes how to use the priority_queue template container of STL with custom types like classes ...
Read more >
Underscore.js
Underscore provides over 100 functions that support both your favorite workaday functional ... This function can currently only compare numbers reliably.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found