Repository navigation
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimehelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is merged
Milestone
Description
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
Logicalis a convenience property equivalent to the result ofCreate(CultureInfo.CurrentCulture, CompareOptions.Logical)LogicalIgnoreCaseis a convenience property equivalent to the result ofCreate(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.1would be considered 4 sequences. TheWindowswould be a string sequence, the8would be a numeric sequence, the.would be another string sequence, and the1would 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
NumberStylesparameter. - 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 number7will be sorted before the lettera. - Existing methods that take a
CompareOptionsparameter as input will need to be updated to support the newLogicalmember.
Open Questions
- Should
CompareOptions.Logicalbe implemented as the flag optionSORT_DIGITSASNUMBERSto thedwCmpFlagsparameter ofCompareStringEx? Using it's implementation should be more efficient but later expanding support forNumberStyleswill require a re-implementation with matching behavior.
Updates
- Added
LogicalandLogicalIgnoreCaseproperties. - Added support for all UTF-16 digits.
- Added more
CreateLogicaloverloads to match theCreatemethod. - Added retrieval of the
NumberFormatInfofrom theStringComparerparameter when not explicitly provided and is aCultureAwareComparer. - Removed
CreateLogicaloverloads that matched theCreatemethod. - 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.Logicaland changedCreateLogicalto be just an overload ofCreate.
Reacted by SunnyWar, Joseph Musser, Tyler Brinkley, Cyrus Najmabadi, John Tur, Artur Baybulatov, endomorphine, Wojciech Litewka, Shimmy Weitzhandler, Ilya and 25 more
Reacted by zotabee
Reacted by iwenr and zotabee
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimehelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is merged
Activity
terrajobst commentedon Jan 13, 2015
That's a good suggestion. Would you be willing to make an API proposal?
MgSam commentedon Jan 22, 2015
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?
sharwell commentedon Jan 24, 2015
While it's easy to define the behavior on Windows systems as following
StrCmpLogicalW, this could be problematic in the long run.StrCmpLogicalWis not only undocumented, but it explicitly states that it can change in future releases of Windows.StringComparer.LogicalIgnoreCasewithout providingStringComparer.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.CurrentCultureorStringComparer.CurrentCultureIgnoreCase, and sequences of digits would be compared according to their numeric values.Peter-Juhasz commentedon Jan 26, 2015
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.
eatdrinksleepcode commentedon Jan 26, 2015
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.
sharwell commentedon Jan 26, 2015
@Peter-Juhasz There is probably no need to consider the
NumberFormatInfo. As stated in the documentation forNumberFormatInfo.NativeDigits:If
StringComparer.LogicalandStringComparer.LogicalIgnoreCasewere defined to useStringComparer.CurrentCultureandStringComparer.CurrentCultureIgnoreCasefor 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
NumberFormatInfoto the comparer since the current culture information already provides it.tarekgh commentedon Aug 16, 2016
Looks in Windows, CompareStringEx can be used with the flag SORT_DIGITSASNUMBERS. I didn't look at ICU if it support such functionality too.
krwq commentedon Nov 28, 2016
I believe there is few things which need to be worked on here:
I think this would be a nice addition but it needs some work.
139 remaining items
kasperk81 commentedon Apr 11, 2024
"how does it work in other languages on various platforms?" has higher precedence for dotnet than win32 behavior
siegfriedpammer commentedon Apr 11, 2024
Yes, as I already pointed out
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.
CyrusNajmabadi commentedon Apr 13, 2024
Could you submit that support to that open source library you reported the bug to?
tompazourek commentedon Apr 14, 2024
I think it's my library and this issue: tompazourek/NaturalSort.Extension#74
I just added the Unicode support.
CyrusNajmabadi commentedon Apr 14, 2024
@tompazourek thanks very much! :)
iSazonov commentedon Apr 15, 2024
@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?
IDisposable commentedon Jul 5, 2024
@tannergooding @tarekgh I would like to take getting this over the finish line... are the API reviews current and approved still?
tarekgh commentedon Jul 5, 2024
This will be risky to do it now. Let's plan doing it early next cycle.
jeffhandley commentedon Nov 7, 2024
Assigning to @PranavSenthilnathan to try to get this in for .NET 10 Preview 1.