Skip to content

MaxBy and MinBy equivalent #895

Closed Answered by JakenVeina
glrusso asked this question in Q&A
Apr 17, 2024 · 5 comments · 2 replies
Discussion options

You must be logged in to vote

Couldn't we treat the Refresh as Remove and then Add?

I suppose you're right, in that the Refresh case can be refined a little further. We can know whether or not the item was previously the max, and when that happens, the only way to determine what the new max should be is to re-aggregate the whole set. However, if the object wasn't the previous max, we only need to check if it exceeds the previous max. So...

case ChangeReason.Refresh:
    itemsByKey[change.Key] = change.Current;
    switch (comparer.Compare(change.Current, max))
    {
        case 0:
            RecalculateMax();
            break;
        case int comparison when comparison > 0:
            max = change.Current;

Replies: 5 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@glrusso
Comment options

Comment options

You must be logged in to vote
1 reply
@glrusso
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by glrusso
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants