String.Compare is not transitive

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Why is <span style="font-family:Consolas; color:#2b91af; font-size:9.5pt String<span style="font-family:Consolas; font-size:9.5pt .Compare(stringA, stringB,
<span style="color:#2b91af StringComparison.InvariantCulture) not trasitive?
<span style="font-family:Consolas; font-size:9.5pt Our system depends on sorted lists but when we use this method for sorting it does not work. Can you supply any extra parameter, that makes String.Compare transitive ?
<span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt [TestClass]
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:blue class
<span style="color:#2b91af CompareStringTest
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue private <span style="color:blue bool isLessThan(<span style="color:blue string a,
<span style="color:blue string b)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue return <span style="color:blue string.Compare(a, b,
<span style="color:#2b91af StringComparison.InvariantCulture) == -1;
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
}
<p style="line-height:normal; margin-bottom:0pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
[TestMethod]
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:blue void TestOrder()
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue string a = <span style="color:#a31515 "Fejl-oprettet";
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span lang="DA" style="font-family:Consolas; color:blue; font-size:9.5pt string<span lang="DA" style="font-family:Consolas; font-size:9.5pt b =
<span style="color:#a31515 "FEJLoprettet";
<p style="line-height:normal; margin-bottom:0pt <span lang="DA" style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue string c = <span style="color:#a31515 "FEJL-oprettet";
<p style="line-height:normal; margin-bottom:0pt <span lang="DA" style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue string d = <span style="color:#a31515 "FEJLOPRETTET";
<p style="line-height:normal; margin-bottom:0pt <span lang="DA" style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue string e = <span style="color:#a31515 "Fejloprettet";
<p style="line-height:normal; margin-bottom:0pt
<p style="line-height:normal; margin-bottom:0pt <span lang="DA" style="font-family:Consolas; font-size:9.5pt <span>
<span style="font-family:Consolas; color:green; font-size:9.5pt //If a < b
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
Assert.IsTrue(isLessThan(a, b));
<p style="line-height:normal; margin-bottom:0pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //and b < c
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
Assert.IsTrue(isLessThan(b, c));
<p style="line-height:normal; margin-bottom:0pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //and c < d
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
Assert.IsTrue(isLessThan(c, d));
<p style="line-height:normal; margin-bottom:0pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //and d < e
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
Assert.IsTrue(isLessThan(d, e));
<p style="line-height:normal; margin-bottom:0pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //then<span> a < e fails ???????
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
Assert.IsTrue(isLessThan(a, e));
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
}
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
}

View the full article
 
Back
Top