473,847 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CaseInsensitive Comparer class, but no
CaseSensitiveCo mparer 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 6465
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.Globaliz ation.CompareOp tions topic."

Hope it helps,

IM

"Adam J. Schaff" <as*****@cascoc dev.com> wrote in message
news:eE******** ******@TK2MSFTN GP12.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 CaseInsensitive Comparer class, but no
CaseSensitiveCo mparer 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.Collecti ons.Comparer & System.Collecti ons.CaseInsensi tiveComparer.
Where Comparer does a case sensitive sort, and CaseInsensitive Comparer 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.Collecti ons.IComparer.C ompare
Dim s1 As String = DirectCast(x, String)
Dim s2 As String = DirectCast(y, String)
Return String.CompareO rdinal(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, "IComparabl e")
list.Sort(CaseI nsensitiveCompa rer.DefaultInva riant)
PrintList(list, "case insensitive")
list.Sort(Compa rer.DefaultInva riant)
PrintList(list, "case sensitive")
list.Sort(New OrdinalComparer )
PrintList(list, "ordinal")

End Sub

Hope this helps
Jay

"Adam J. Schaff" <as*****@cascoc dev.com> wrote in message
news:uP******** *******@TK2MSFT NGP12.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
14657
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 allow entry to one or the other or Both at the same time. Now I tried to use an If ElseIf but it got too hard to track, so now I am using a SELECT CASE Statement.
19
8120
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
2050
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 to the end of the select case block, instead of finishing going thru the rest of the cases (and thereby reaching the next "OTHER" case). Can anyone explain this to me?? Thanks! I've put ***** where things seems to mess up! Select Case...
2
3211
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 below:- <% tBegin = request("beginww") tEnd = request("endww") tYear = request("wwyear") %> <body>
6
4705
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 <iomanip> #include <iostream> using namespace std;
10
2179
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
9345
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
3188
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 numbers then added support for putting in lat/lon coordinates. I tried this little piece of code inside the OnChar function but compiler complained about missing ';' after "case _T('W'):"
17
2666
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
3140
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 showAge that uses a select case structure to print out the following: If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager. If the age is 20 - 40 print "You are an adult" anything else print "You are...
0
9727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10981
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10643
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5907
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4113
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.