Skip to content

API proposal: Comparison by key #33877

Closed
Closed

Description

@jnm2

Before

You have to write out the selector twice:

names.Sort((first, second) => string.Compare(
    first.Identifier.GetIdentifierText(),
    second.Identifier.GetIdentifierText(),
    StringComparer.OrdinalIgnoreCase));

(Comparer.Sequence is #33873)

statements.OrderBy(GetNameSegments, Comparer.Sequence(
    Comparer.Create((first, second) => string.Compare(
        first.Identifier.GetIdentifierText(),
        second.Identifier.GetIdentifierText(),
        StringComparer.OrdinalIgnoreCase)))

After

// Proposal 1
names.Sort(Comparer.By(
    (SimpleNameSyntax name) => name.Identifier.GetIdentifierText(),
    StringComparer.OrdinalIgnoreCase));

// Proposal 2
names.Sort(Comparer<SimpleNameSyntax>.By(
    name => name.Identifier.GetIdentifierText(),
    StringComparer.OrdinalIgnoreCase))
// Proposal 1
statements.OrderBy(GetNameSegments, Comparer.Sequence(Comparer.By(
    (SimpleNameSyntax name) => name.Identifier.GetIdentifierText(),
    StringComparer.OrdinalIgnoreCase));

// Proposal 2
statements.OrderBy(GetNameSegments, Comparer.Sequence(Comparer<SimpleNameSyntax>.By(
    name => name.Identifier.GetIdentifierText(),
    StringComparer.OrdinalIgnoreCase))

Proposal 1

namespace System.Collections
{
    public sealed class Comparer : IComparer, ISerializable
    {
+       public static IComparer<T> By<T, TKey>(
+           Func<T, TKey> keySelector,
+           IComparer<TKey>? keyComparer = null);

        // No other static members are declared by Comparer
    }
}

Proposal 2

namespace System.Collections.Generic
{
    public abstract partial class Comparer<T> : IComparer, IComparer<T>
    {
+       public static IComparer<T> By<TKey>(
+           Func<T, TKey> keySelector,
+           IComparer<TKey>? keyComparer = null);

        // Other static members declared by Comparer:
        public static Comparer<T> Create(Comparison<T> comparison);
    }
}

Draft implementation

Click to expand
private sealed class KeyComparer<T, TKey> : IComparer<T>
{
    private readonly Func<T, TKey> keySelector;
    private readonly IComparer<TKey> keyComparer;

    public KeyComparer(Func<T, TKey> keySelector, IComparer<TKey> keyComparer)
    {
        this.keySelector = keySelector;
        this.keyComparer = keyComparer;
    }

    public int Compare([AllowNull] T x, [AllowNull] T y)
    {
        return keyComparer.Compare(
            keySelector.Invoke(x!),
            keySelector.Invoke(y!));
    }
}

Activity

  1. added
    untriaged
    on Mar 20, 2020
  2. svick commented on Apr 11, 2020

    @svick
    Member

    I definitely like this idea, though I think it becomes much more valuable if you add the ability to compare multiple keys. That would mean instead of:

    people.Sort((x, y) =>
    {
        int result = x.LastName.CompareTo(y.LastName);
        
        if (result == 0)
            result = x.FirstName.CompareTo(y.FirstName);
            
        if (result == 0)
            result = x.MiddleName.CompareTo(y.MiddleName);
    
        return result;
    });

    you could write something like:

    people.Sort(KeyComparer<Person>.OrderBy(p => p.LastName)
                                   .ThenBy(p => p.FirstName)
                                   .ThenBy(p => p.MiddleName));

    A NuGet package that implements this already exists, though, to my dismay, it's not very popular.

  3. jnm2 commented on Apr 11, 2020

    @jnm2
    ContributorAuthor

    @svick One thing I've been favoring even with LINQ instead of ThenBy is to select a tuple:

    people.Sort(Comparer<Person>.By(p => (p.LastName, p.FirstName, p.MiddleName)));

    The only thing this doesn't let you do is specify a comparer for each tuple element, but that's a proposal I have on my list to file separately from this because I want it without selectors too.

  4. added
    api-suggestion
    and removed
    untriaged
    on Jul 7, 2020
  5. added this to the Future milestone on Jul 7, 2020
  6. added
    needs-further-triage
    and removed on Nov 23, 2020
  7. eiriktsarpalis commented on Nov 23, 2020

    @eiriktsarpalis
    Member

    I would probably prefer proposal 2, since the untyped Comparer type lives in the System.Collections namespace.

  8. lbmaian commented on Dec 1, 2020

    @lbmaian

    A similar API for EqualityComparer would also be a nice counterpart to this proposal. Just replace IComparer with IEqualityComparer and have an internal KeyEqualityComparer class implement the latter.

  9. added
    untriaged
    and removed
    needs-further-triage
    on Apr 19, 2021
  10. 1 remaining item

  11. added
    api-needs-work
    and removed
    untriaged
    on Apr 19, 2021
  12. removed
    api-suggestion
    on Jun 25, 2021
  13. eiriktsarpalis commented on Oct 29, 2021

    @eiriktsarpalis
    Member

    I think we might need to submit an API proposal that merges this with #33873 so that we can land on a holistic design.

  14. jnm2 commented on Oct 29, 2021

    @jnm2
    ContributorAuthor

    @eiriktsarpalis I also want to propose a value tuple comparer so that it's possible to e.g. specify OrdinalIgnoreCase for Dictionary<(string A, int B), Foo>. Should that be part of the holistic design? I can write it up if so.

    ValueTupleComparer.Create(StringComparer.OrdinalIgnoreCase, EqualityComparer<int>.Default) or similar, so that both IComparer<T> and IEqualityComparer<T> can be implemented maybe.

  15. eiriktsarpalis commented on Oct 29, 2021

    @eiriktsarpalis
    Member

    Go for it. I would recommend creating a fresh issue using the API proposal template so we can start from scratch. Please also check if there are other related issues in the backlog so we can consider including them in the design.

  16. eiriktsarpalis commented on Oct 29, 2021

    @eiriktsarpalis
    Member

    @jnm2 there are a few IEqualityComparer<T> requests as well lurking in the backlog. Would it make sense to merge everything in one big Comparer/EqualityComparer factories proposal? For example there's #44796 and #19644.

  17. jnm2 commented on Oct 29, 2021

    @jnm2
    ContributorAuthor

    @eiriktsarpalis I'm not sure, what's been done before that's similar?

  18. eiriktsarpalis commented on Nov 1, 2021

    @eiriktsarpalis
    Member

    I would probably look at the static APIs in the Comparer<T> and EqualityComparer<T> classes.

  19. dotnet-policy-service commented on Oct 19, 2026

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  20. added
    backlog-cleanup-candidate
    on Oct 19, 2026
  21. dotnet-policy-service commented on Nov 16, 2026

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  22. removed this from the Future milestone on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.Collectionsbacklog-cleanup-candidateno-recent-activity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions