public override int CompareTo(object obj) { XmlStringSortKey that = obj as XmlStringSortKey; int idx, cntCmp, result; if (that == null) { return(CompareToEmpty(obj)); } // Compare either using SortKey.Compare or byte arrays if (_sortKey != null) { Debug.Assert(that._sortKey != null, "Both keys must have non-null sortKey field"); result = SortKey.Compare(_sortKey, that._sortKey); } else { Debug.Assert(_sortKeyBytes != null && that._sortKeyBytes != null, "Both keys must have non-null sortKeyBytes field"); cntCmp = (_sortKeyBytes.Length < that._sortKeyBytes.Length) ? _sortKeyBytes.Length : that._sortKeyBytes.Length; for (idx = 0; idx < cntCmp; idx++) { if (_sortKeyBytes[idx] < that._sortKeyBytes[idx]) { result = -1; goto Done; } if (_sortKeyBytes[idx] > that._sortKeyBytes[idx]) { result = 1; goto Done; } } // So far, keys are equal, so now test length of each key if (_sortKeyBytes.Length < that._sortKeyBytes.Length) { result = -1; } else if (_sortKeyBytes.Length > that._sortKeyBytes.Length) { result = 1; } else { result = 0; } } Done: // Use document order to break sorting tie if (result == 0) { return(BreakSortingTie(that)); } return(_descendingOrder ? -result : result); }
//https://docs.microsoft.com/en-us/globalization/locale/sorting-and-string-comparison public static bool IsInAlphabeticalOrder(IList <string> list, string cultureCode, out string message) { var compareInfo = CultureInfo.GetCultureInfo(cultureCode).CompareInfo; message = null; var current = list.First(); foreach (var item in list.Skip(1)) { var next = item; var sc1 = compareInfo.GetSortKey(current); var sc2 = compareInfo.GetSortKey(next); var result = SortKey.Compare(sc1, sc2); if (result > 0) { message = $"Alphabetical order problem: '{next}' is before than '{current}'"; return(false); } current = next; } return(true); }
public void Compare_secondnull_throws() { byte[] keyData = new byte[] { 0xae, 0x1, 0x20, 0x1 }; SortKey sortKey = Collator.CreateSortKey("heo", keyData); Assert.Throws <ArgumentNullException>(() => SortKey.Compare(sortKey, null)); }
public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expectedSign) { SortKey sk1 = compareInfo.GetSortKey(string1, options); SortKey sk2 = compareInfo.GetSortKey(string2, options); Assert.Equal(expectedSign, Math.Sign(SortKey.Compare(sk1, sk2))); Assert.Equal(expectedSign == 0, sk1.Equals(sk2)); if (!WindowsVersionHasTheCompareStringRegression) { Assert.Equal(Math.Sign(compareInfo.Compare(string1, string2, options)), Math.Sign(SortKey.Compare(sk1, sk2))); } Assert.Equal(compareInfo.GetHashCode(string1, options), sk1.GetHashCode()); Assert.Equal(compareInfo.GetHashCode(string2, options), sk2.GetHashCode()); Assert.Equal(string1, sk1.OriginalString); Assert.Equal(string2, sk2.OriginalString); // Now try the span-based versions - use BoundedMemory to detect buffer overruns RunSpanSortKeyTest(compareInfo, string1, options, sk1.KeyData); RunSpanSortKeyTest(compareInfo, string2, options, sk2.KeyData); unsafe static void RunSpanSortKeyTest(CompareInfo compareInfo, ReadOnlySpan <char> source, CompareOptions options, byte[] expectedSortKey)
public void Compare_firstnull_throws() { byte[] keyData = new byte[] { 0xae, 0x1, 0x20, 0x1 }; SortKey sortKey = Collator.CreateSortKey("heo", keyData); SortKey.Compare(null, sortKey); }
public void Compare_secondnull_throws() { byte[] keyData = new byte[] { 0xae, 0x1, 0x20, 0x1 }; SortKey sortKey = Collator.CreateSortKey("heo", keyData); SortKey.Compare(sortKey, null); }
public int Compare(string str1, string str2) { SortKey sk1, sk2; sk1 = CultureInfo.CurrentCulture.CompareInfo.GetSortKey(str1); sk2 = CultureInfo.CurrentCulture.CompareInfo.GetSortKey(str2); return(SortKey.Compare(sk1, sk2)); }
public void Compare_keyDataSame_same() { byte[] keyData = new byte[] { 0xae, 0x1, 0x20, 0x1 }; SortKey sortKey1 = Collator.CreateSortKey("heo", keyData); SortKey sortKey2 = Collator.CreateSortKey("heol", keyData); Assert.AreEqual(Same, SortKey.Compare(sortKey1, sortKey2)); }
public void GetSortKey() { using (var serbianCollator = new RuleBasedCollator(SerbianRules)) { var sortKeyČUKIĆ = serbianCollator.GetSortKey("ČUKIĆ SLOBODAN"); var sortKeyCUKIĆ = serbianCollator.GetSortKey("CUKIĆ SVETOZAR"); Assert.That(SortKey.Compare(sortKeyČUKIĆ, sortKeyCUKIĆ), Is.EqualTo(1)); } }
public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expected) { SortKey sk1 = compareInfo.GetSortKey(string1, options); SortKey sk2 = compareInfo.GetSortKey(string2, options); Assert.Equal(expected, SortKey.Compare(sk1, sk2)); Assert.Equal(string1, sk1.OriginalString); Assert.Equal(string2, sk2.OriginalString); }
public void Compare_keyDataByteChanges_NotAffected() { byte[] keyData = new byte[] { 0xae, 0x1, 0x20, 0x1 }; SortKey sortKey1 = Collator.CreateSortKey("heo", keyData); keyData[2] = 0x21; SortKey sortKey2 = Collator.CreateSortKey("hao", keyData); Assert.AreEqual(Precedes, SortKey.Compare(sortKey1, sortKey2)); }
public void Compare_SecondLesser_follows() { byte[] keyData = new byte[] { 0xae, 0x1, 0x20, 0x1 }; SortKey sortKey1 = Collator.CreateSortKey("heol", keyData); keyData[0] = 0xad; SortKey sortKey2 = Collator.CreateSortKey("heo", keyData); Assert.AreEqual(Follows, SortKey.Compare(sortKey1, sortKey2)); }
public void Compare_SecondGreater_precedes() { byte[] keyData = new byte[] { 0xae, 0x1, 0x20, 0x1 }; SortKey sortKey1 = Collator.CreateSortKey("heol", keyData); keyData[0] = 0xaf; SortKey sortKey2 = Collator.CreateSortKey("heo", keyData); Assert.AreEqual(Precedes, SortKey.Compare(sortKey1, sortKey2)); }
public void Compare_SamePrefixSecondShorter_follows() { byte[] keyData1 = new byte[] { 0xae, 0x1, 0x20, 0x30, 0x1 }; SortKey sortKey1 = Collator.CreateSortKey("heol", keyData1); byte[] keyData2 = new byte[] { 0xae, 0x1, 0x20, 0x1 }; SortKey sortKey2 = Collator.CreateSortKey("heo", keyData2); Assert.AreEqual(Follows, SortKey.Compare(sortKey1, sortKey2)); }
public void Compare_SamePrefixSecondLonger_precedes() { byte[] keyData1 = new byte[] { 0xae, 0x1, 0x20, 0x1 }; SortKey sortKey1 = Collator.CreateSortKey("heo", keyData1); byte[] keyData2 = new byte[] { 0xae, 0x1, 0x20, 0x32, 0x1 }; SortKey sortKey2 = Collator.CreateSortKey("heol", keyData2); Assert.AreEqual(Precedes, SortKey.Compare(sortKey1, sortKey2)); }
// Methods :: Public // Methods :: Public :: CompareTo (IComparable) public int CompareTo(object o) { // Check if we're trying to compare to nothing if (o == null) { return(1); } Item other = (Item)o; return(SortKey.Compare(this.SortKey, other.SortKey)); }
private static int CompareStringsUnicode(String str1, int start1, out int endPos1, String str2, int start2, out int endPos2) { var s1 = CreateStringFromJSON(str1, start1, out endPos1); var s2 = CreateStringFromJSON(str2, start2, out endPos2); // TODO: Detect current localization and use the corresponding CompareInfo var comp = CultureInfo.InvariantCulture.CompareInfo; var sk1 = comp.GetSortKey(s1); var sk2 = comp.GetSortKey(s2); return(SortKey.Compare(sk1, sk2)); }
public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expectedSign) { SortKey sk1 = compareInfo.GetSortKey(string1, options); SortKey sk2 = compareInfo.GetSortKey(string2, options); Assert.Equal(expectedSign, Math.Sign(SortKey.Compare(sk1, sk2))); Assert.Equal(expectedSign == 0, sk1.Equals(sk2)); Assert.Equal(Math.Sign(compareInfo.Compare(string1, string2, options)), Math.Sign(SortKey.Compare(sk1, sk2))); Assert.Equal(compareInfo.GetHashCode(string1, options), sk1.GetHashCode()); Assert.Equal(compareInfo.GetHashCode(string2, options), sk2.GetHashCode()); Assert.Equal(string1, sk1.OriginalString); Assert.Equal(string2, sk2.OriginalString); }
private static int CompareStringsUnicode(String str1, int start1, out int endPos1, String str2, int start2, out int endPos2) { var s1 = CreateStringFromJSON(str1, start1, out endPos1); var s2 = CreateStringFromJSON(str2, start2, out endPos2); #if PORTABLE //TODO: Fix CompareStringsUnicode for PCL //Not sure this is really the same as the SortKey method // Need to research and fix return(String.CompareOrdinal(s1, s2)); #else // TODO: Detect current localization and use the corresponding CompareInfo var comp = CultureInfo.InvariantCulture.CompareInfo; var sk1 = comp.GetSortKey(s1); var sk2 = comp.GetSortKey(s2); return(SortKey.Compare(sk1, sk2)); #endif }
public static void Main(String[] args) { String str1 = "Apple"; String str2 = "Æble"; // Set the CurrentCulture to "da-DK". CultureInfo dk = new CultureInfo("da-DK"); Thread.CurrentThread.CurrentCulture = dk; // Create a culturally sensitive sort key for str1. SortKey sc1 = dk.CompareInfo.GetSortKey(str1); // Create a culturally sensitive sort key for str2. SortKey sc2 = dk.CompareInfo.GetSortKey(str2); // Compare the two sort keys and display the results. int result1 = SortKey.Compare(sc1, sc2); Console.WriteLine("When the CurrentCulture is \"da-DK\","); Console.WriteLine("the result of comparing {0} with {1} is: {2}\n", str1, str2, result1); // Set the CurrentCulture to "en-US". CultureInfo enus = new CultureInfo("en-US"); Thread.CurrentThread.CurrentCulture = enus; // Create a culturally sensitive sort key for str1. SortKey sc3 = enus.CompareInfo.GetSortKey(str1); // Create a culturally sensitive sort key for str1. SortKey sc4 = enus.CompareInfo.GetSortKey(str2); // Compare the two sort keys and display the results. int result2 = SortKey.Compare(sc3, sc4); Console.WriteLine("When the CurrentCulture is \"en-US\","); Console.WriteLine("the result of comparing {0} with {1} is: {2}", str1, str2, result2); }
public static void Main() { string lowerABC = "abc"; string upperABC = "ABC"; int result = 0; // Create a CompareInfo object for the en-US culture. Console.WriteLine("\nCreate a CompareInfo object for the en-US culture...\n"); CompareInfo cmpi = CompareInfo.GetCompareInfo("en-US"); // Alternatively: // CompareInfo cmpi = new CultureInfo("en-US").CompareInfo; // Create sort keys for lowercase and uppercase "abc", the en-US culture, and // ignore case. SortKey sk1LowerIgnCase = cmpi.GetSortKey(lowerABC, CompareOptions.IgnoreCase); SortKey sk2UpperIgnCase = cmpi.GetSortKey(upperABC, CompareOptions.IgnoreCase); // Create sort keys for lowercase and uppercase "abc", the en-US culture, and // use case. SortKey sk1LowerUseCase = cmpi.GetSortKey(lowerABC, CompareOptions.None); SortKey sk2UpperUseCase = cmpi.GetSortKey(upperABC, CompareOptions.None); // Compare lowercase and uppercase "abc", ignoring case and using CompareInfo. result = cmpi.Compare(lowerABC, upperABC, CompareOptions.IgnoreCase); Display(result, "CompareInfo, Ignore case", lowerABC, upperABC); // Compare lowercase and uppercase "abc", ignoring case and using SortKey. result = SortKey.Compare(sk1LowerIgnCase, sk2UpperIgnCase); Display(result, "SortKey, Ignore case", lowerABC, upperABC); Console.WriteLine(); // Compare lowercase and uppercase "abc", using case and using CompareInfo. result = cmpi.Compare(lowerABC, upperABC, CompareOptions.None); Display(result, "CompareInfo, Use case", lowerABC, upperABC); // Compare lowercase and uppercase "abc", using case and using SortKey. result = SortKey.Compare(sk1LowerUseCase, sk2UpperUseCase); Display(result, "SortKey, Use case", lowerABC, upperABC); }
public void Compare_bothnull_throws() { Assert.Throws <ArgumentNullException>(() => SortKey.Compare(null, null)); }
public void CompareNull() { // bug #376171 SortKey.Compare(null, null); }
public void CompareNull2() { // bug #376171 SortKey.Compare(CultureInfo.InvariantCulture.CompareInfo.GetSortKey("A"), null); }
public void Compare_bothnull_throws() { SortKey.Compare(null, null); }
static void Main(string[] args) { CompareInfo myComp_enUS = new CultureInfo("en-US", false).CompareInfo; SortKey mySKl = myComp_enUS.GetSortKey("llama"); CompareInfo myComp_esEs = new CultureInfo("es-ES", false).CompareInfo; SortKey mySK2 = myComp_esEs.GetSortKey("llama"); CompareInfo myComp_es = new CultureInfo(0x040A, false).CompareInfo; SortKey mySK3 = myComp_es.GetSortKey("llama"); Console.WriteLine("Comparing \"llama\" in en-US and in es-ES with international sort : {0}", SortKey.Compare(mySKl, mySK2)); Console.WriteLine("Comparing \"llama\" in en-US and in es-ES with traditional sort : {0}", SortKey.Compare(mySKl, mySK3)); }
public static void Main() { // Creates a SortKey using the en-US culture. CompareInfo myComp_enUS = new CultureInfo("en-US", false).CompareInfo; SortKey mySK1 = myComp_enUS.GetSortKey("llama"); // Creates a SortKey using the es-ES culture with international sort. CompareInfo myComp_esES = new CultureInfo("es-ES", false).CompareInfo; SortKey mySK2 = myComp_esES.GetSortKey("llama"); // Creates a SortKey using the es-ES culture with traditional sort. CompareInfo myComp_es = new CultureInfo(0x040A, false).CompareInfo; SortKey mySK3 = myComp_es.GetSortKey("llama"); // Compares the en-US SortKey with each of the es-ES SortKey objects. Console.WriteLine("Comparing \"llama\" in en-US and in es-ES with international sort : {0}", SortKey.Compare(mySK1, mySK2)); Console.WriteLine("Comparing \"llama\" in en-US and in es-ES with traditional sort : {0}", SortKey.Compare(mySK1, mySK3)); }