Skip to content

String comparer for sorting numeric strings logically #13979

Closed

Description

@Peter-Juhasz

Rationale

For sorting purposes it's common to need portions of strings containing numbers to be treated like numbers. Consider the list of strings "Windows 7", "Windows 10".

Using the Ordinal StringComparer to sort the list one would get

Windows 10
Windows 7

but the desired ascending logical sort would be

Windows 7
Windows 10

Proposed API

 namespace System {
     public class StringComparer {
+        public static StringComparer Create(CultureInfo culture, CompareOptions options);
     }
 }
 namespace System.Globalization {
     public enum CompareOptions {
+        NumericOrdering = 0x00000020
     }
 }

Usage

var list = new List<string> { "Windows 10", "Windows 7" };
list.Sort(StringComparer.Logical); // List is now "Windows 7", "Windows 10"

This would also be good for sorting strings containing IP addresses.

Details

  • Logical is a convenience property equivalent to the result of Create(CultureInfo.CurrentCulture, CompareOptions.Logical)
  • LogicalIgnoreCase is a convenience property equivalent to the result of Create(CultureInfo.CurrentCulture, CompareOptions.Logical | CompareOptions.IgnoreCase)
  • Non-numeric sequences will be evaluated with the culture provided.
  • Numeric sequences will be determined by the result of Char.IsDigit.
  • All UTF-16 digits will be supported and are manually parsed using Char.GetNumericValue.
  • Only positive integral values without digit separators will be supported directly.
  • Numbers will be treated as ulongs. Logic for overflows will have to be considered.
  • The string Windows 8.1 would be considered 4 sequences. The Windows would be a string sequence, the 8 would be a numeric sequence, the . would be another string sequence, and the 1 would be another numeric sequence.
  • This API could later be expanded to include support for allowing signs, decimals, and digit separators through the use of overloads accepting a NumberStyles parameter.
  • When a numeric and string sequence are considered at the same time the numeric sequence always comes before the string sequence so when sorting the following list, "a", "7" the number 7 will be sorted before the letter a.
  • Existing methods that take a CompareOptions parameter as input will need to be updated to support the new Logical member.

Open Questions

  • Should CompareOptions.Logical be implemented as the flag option SORT_DIGITSASNUMBERS to the dwCmpFlags parameter of CompareStringEx? Using it's implementation should be more efficient but later expanding support for NumberStyles will require a re-implementation with matching behavior.

Updates

  • Added Logical and LogicalIgnoreCase properties.
  • Added support for all UTF-16 digits.
  • Added more CreateLogical overloads to match the Create method.
  • Added retrieval of the NumberFormatInfo from the StringComparer parameter when not explicitly provided and is a CultureAwareComparer.
  • Removed CreateLogical overloads that matched the Create method.
  • Switched to only supporting positive integral values without digit separators.
  • Added consideration of comparing a numeric sequence with a string sequence.
  • Added the flag member CompareOptions.Logical and changed CreateLogical to be just an overload of Create.

Activity

  1. terrajobst commented on Jan 13, 2015

    @terrajobst
    Contributor

    That's a good suggestion. Would you be willing to make an API proposal?

  2. MgSam commented on Jan 22, 2015

    @MgSam

    I think conceptually this is a good idea, but there are a lot of details that need to be worked out. It seems like such a comparer might want to take options which control how it behaves. For example, are commas in numbers ok? Spaces? Decimal points? How are different cultures handled?

  3. sharwell commented on Jan 24, 2015

    @sharwell
    Contributor

    While it's easy to define the behavior on Windows systems as following StrCmpLogicalW, this could be problematic in the long run.

    1. The precise behavior of StrCmpLogicalW is not only undocumented, but it explicitly states that it can change in future releases of Windows.
    2. The behavior of this method is not case-sensitive, and there is no case-sensitive method which is otherwise equivalent. It would be strange, for example, to provide StringComparer.LogicalIgnoreCase without providing StringComparer.Logical.

    I believe you could define a reasonable API proposal in terms of a lexicographical ordering of substrings divided at numeric boundaries. Sequences of non-digits would be compared using StringComparer.CurrentCulture or StringComparer.CurrentCultureIgnoreCase, and sequences of digits would be compared according to their numeric values.

  4. Peter-Juhasz commented on Jan 26, 2015

    @Peter-Juhasz
    Author

    The comparer should require an IFormatProvider or NumberFormatInfo; which can be passed in the constructor or it should use the current one from the owning thread.

    @terrajobst This is a problem I am facing in almost every business application: proper ordering even if the strings contain numbers. I don't have time to make a PR or document it well, but I would be very happy to see it in the new releases of the core framework. I think the use case is clear enough.

  5. eatdrinksleepcode commented on Jan 26, 2015

    @eatdrinksleepcode

    This is a need that I have often faced. If @Peter-Juhasz does not have the time to create the API proposal or implement it, I would be interested in taking it on.

  6. sharwell commented on Jan 26, 2015

    @sharwell
    Contributor

    @Peter-Juhasz There is probably no need to consider the NumberFormatInfo. As stated in the documentation for NumberFormatInfo.NativeDigits:

    The character set that is specified by the NativeDigits property has no effect on parsing or formatting operations. Only the Basic Latin digits 0 (U+0030) through 9 (U+0039) are used when formatting or parsing numeric values or date and time values.

    If StringComparer.Logical and StringComparer.LogicalIgnoreCase were defined to use StringComparer.CurrentCulture and StringComparer.CurrentCultureIgnoreCase for comparing non-digits, the culture used for the comparison is clear.

    On a side note, even if used there is no need to explicitly provide the NumberFormatInfo to the comparer since the current culture information already provides it.

  7. assigned and unassigned on Aug 3, 2016
  8. tarekgh commented on Aug 16, 2016

    @tarekgh
    Member

    Looks in Windows, CompareStringEx can be used with the flag SORT_DIGITSASNUMBERS. I didn't look at ICU if it support such functionality too.

  9. assigned and unassigned on Nov 8, 2016
  10. krwq commented on Nov 28, 2016

    @krwq
    Member

    I believe there is few things which need to be worked on here:

    • check if there is linux implementation existing and if it behaves similarly to CompareStringEx with SORT_DIGITSASNUMBERS
    • should we provide all permutations for all comparers now or perhaps generate comparer based on some settings (perhaps we already have something like that which we might be able to inject this log in and I might not be aware of it)

    I think this would be a nice addition but it needs some work.

  11. removed their assignment
    on Nov 28, 2016
  12. 139 remaining items

  13. kasperk81 commented on Apr 11, 2024

    @kasperk81
    Contributor

    "how does it work in other languages on various platforms?" has higher precedence for dotnet than win32 behavior

  14. siegfriedpammer commented on Apr 11, 2024

    @siegfriedpammer

    Yes, as I already pointed out

    I agree with @sharwell that using StrCmpLogicalW as a reference is problematic, but my point still stands that correct Unicode support is necessary but tricky.

    I am solely interested in getting proper Unicode support and the Win API does a better job at that than the alternatives I found. At this point for me it's only about Unicode and nothing else.

  15. CyrusNajmabadi commented on Apr 13, 2024

    @CyrusNajmabadi
    Contributor

    Could you submit that support to that open source library you reported the bug to?

  16. tompazourek commented on Apr 14, 2024

    @tompazourek

    Could you submit that support to that open source library you reported the bug to?

    I think it's my library and this issue: tompazourek/NaturalSort.Extension#74

    I just added the Unicode support.

  17. CyrusNajmabadi commented on Apr 14, 2024

    @CyrusNajmabadi
    Contributor

    @tompazourek thanks very much! :)

  18. iSazonov commented on Apr 15, 2024

    @iSazonov
    Contributor

    @tannergooding @tarekgh ICU is now well integrated into Runtime and probably most of the work has already been done. Is there a chance to include this in .Net 9?

  19. IDisposable commented on Jul 5, 2024

    @IDisposable
    Contributor

    @tannergooding @tarekgh I would like to take getting this over the finish line... are the API reviews current and approved still?

  20. tarekgh commented on Jul 5, 2024

    @tarekgh
    Member

    This will be risky to do it now. Let's plan doing it early next cycle.

  21. modified the milestones: Future, 10.0.0 on Nov 7, 2024
  22. jeffhandley commented on Nov 7, 2024

    @jeffhandley
    Member

    Assigning to @PranavSenthilnathan to try to get this in for .NET 10 Preview 1.

  23. added
    in-pr
    on Nov 25, 2024
  24. locked and limited conversation to collaborators on Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedarea-System.Runtimehelp wantedin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions