By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,145 Members | 1,565 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,145 IT Pros & Developers. It's quick & easy.

How can I do a case-sensitive sort of an ArrayList ?

P: n/a
Hello. I recently noticed that the Sort method of the .NET ArrayList class
does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii
values) but what I got was the opposite. Simple code:

Dim y As New ArrayList
y.Add("Abc")

y.Add("abc")

y.Add("_bc")

y.Sort()

Examine y and you'll see that 'Abc' is last rather than first. So I'm
guessing from this that the default behavior is to do a case-insensitive
sort. Does anyone know how I can get it to do a case-sensitive sort?
Ironically, I found a CaseInsensitiveComparer class, but no
CaseSensitiveComparer class, so I'm not sure whether the IComparer overload
of the Sort method would somehow be helpful.

Any help would be much appereciated.

-AJ
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Have a look at this:
(
Taken from microsoft's documentation:
http://msdn.microsoft.com/library/de...classtopic.asp
)
"Sort rules determine the alphabetic order of Unicode characters and how two
strings compare to each other. For example, the Compare method performs a
linguistic comparison while the CompareOrdinal method performs an ordinal
comparison. Consequently, if the current culture is U.S. English, the
Compare method considers 'a' less than 'A' while the CompareOrdinal method
considers 'a' greater than 'A'.

The .NET Framework supports word, string, and ordinal sort rules. A word
sort performs a culture-sensitive comparison of strings in which certain
nonalphanumeric Unicode characters might have special weights assigned to
them. For example, the hyphen ("-") might have a very small weight assigned
to it so that "coop" and "co-op" appear next to each other in a sorted list.
A string sort is similar to a word sort, except that there are no special
cases and all nonalphanumeric symbols come before all alphanumeric Unicode
characters. An ordinal sort compares strings based on the numeric value of
each Char in the string. For more information about word, string, and
ordinal sort rules, see the System.Globalization.CompareOptions topic."

Hope it helps,

IM

"Adam J. Schaff" <as*****@cascocdev.com> wrote in message
news:eE**************@TK2MSFTNGP12.phx.gbl...
Hello. I recently noticed that the Sort method of the .NET ArrayList class
does not behave as I expected. I expect 'A' < '_' < 'a' (as per their
ascii values) but what I got was the opposite. Simple code:

Dim y As New ArrayList
y.Add("Abc")

y.Add("abc")

y.Add("_bc")

y.Sort()

Examine y and you'll see that 'Abc' is last rather than first. So I'm
guessing from this that the default behavior is to do a case-insensitive
sort. Does anyone know how I can get it to do a case-sensitive sort?
Ironically, I found a CaseInsensitiveComparer class, but no
CaseSensitiveComparer class, so I'm not sure whether the IComparer
overload of the Sort method would somehow be helpful.

Any help would be much appereciated.

-AJ

Nov 21 '05 #2

P: n/a
Sounds like an ordinal sort might be what I want. Can anyone show me how to
force ArrayList.Sort to perform an ordinal sort?
Nov 21 '05 #3

P: n/a
Adam,
The ArrayList.Sort methods supports an IComparer parameter that indicates
how to compare values.

If you do not pass an IComparer value it uses the IComparable of each
element (in other words it asks each string to compare itself to the next
one).

..NET defines two IComparer classes that you would use with ArrayList.Sort
System.Collections.Comparer & System.Collections.CaseInsensitiveComparer.
Where Comparer does a case sensitive sort, and CaseInsensitiveComparer does
a case insensitive sort.

If you truly want an Ordinal sort I would define my own Comparer

Something like:

Public Class OrdinalComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collections.IComparer.Compare
Dim s1 As String = DirectCast(x, String)
Dim s2 As String = DirectCast(y, String)
Return String.CompareOrdinal(s1, s2)
End Function

End Class

Private Shared Sub PrintList(ByVal list As ArrayList, ByVal category As
String)
For Each value As String In list
Debug.WriteLine(value, category)
Next
End Sub

Public Shared Sub Main()
Dim list As New ArrayList
list.Add("Abc")
list.Add("abc")
list.Add("_bc")
list.Sort()
PrintList(list, "IComparable")
list.Sort(CaseInsensitiveComparer.DefaultInvariant )
PrintList(list, "case insensitive")
list.Sort(Comparer.DefaultInvariant)
PrintList(list, "case sensitive")
list.Sort(New OrdinalComparer)
PrintList(list, "ordinal")

End Sub

Hope this helps
Jay

"Adam J. Schaff" <as*****@cascocdev.com> wrote in message
news:uP***************@TK2MSFTNGP12.phx.gbl...
Sounds like an ordinal sort might be what I want. Can anyone show me how
to force ArrayList.Sort to perform an ordinal sort?

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.