-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Runtimehelp wanted[up-for-grabs] Good issue for external contributors[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is mergedThere 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.
SunnyWar, jnm2, TylerBrinkley, CyrusNajmabadi, reflectronic and 30 morezotabeeiwenr and zotabee
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Runtimehelp wanted[up-for-grabs] Good issue for external contributors[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged