473,766 Members | 2,064 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 6451
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
14642
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
8106
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
2044
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
3209
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
4702
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
2167
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
9338
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
3173
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
2656
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
3127
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
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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
10008
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...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
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...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.