473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
3 6376
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Newbie | last post by:
Dear friends, I am having a hard time understanding how to use a SELECT CASE in ASP. I have used it in VB but never in ASP scripting. Scenerio: I have 2 textboxes on a form that I have to...
19
by: Robert Scheer | last post by:
Hi. In VBScript I can use a Select Case statement like that: Select Case X Case 1 to 10 'X is between 1 and 10 Case 11,14,16 'X is 11 or 14 or 16 End Select
1
by: ST | last post by:
Hi, I'm trying to debug someone else's code, and I'm going thru this Select Case statement. I'm having problems with the "OTHER" case...in that when the first line of the case is false, it jumps...
2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
7
by: Lauren Quantrell | last post by:
Is there any speed/resource advantage/disadvantage in using Select Case x Case 1 Case 2 etc. many more cases... End Select VS.
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
17
by: Navodit | last post by:
So I have some code like: if (document.Insurance.State.selectedIndex == 1) { ifIll(); } else if (document.Insurance.State.selectedIndex == 2) { elseKan(); }
24
by: clockworx05 | last post by:
Hey guys i have this program that i need to write for class. here are the instructions: Write a function called foo that asks the user for their age. Pass the age value to a function called...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.