char[] c = { 'a', 'b', 'c', 'A', 'B', 'C' };
Array.Sort(c, CaseInsensitiveComparer.Default);
After this, c is ordered { 'A', 'B', 'C', 'a', 'b', 'c' }.
I want letters to be sorted together regardless of case, e.g. 'A' next to
'a'.
Why doesn't that case-insensitive sort work? What should I do instead?
Eq. 2 4299
Paul E Collins wrote:
char[] c = { 'a', 'b', 'c', 'A', 'B', 'C' };
Array.Sort(c, CaseInsensitiveComparer.Default);
After this, c is ordered { 'A', 'B', 'C', 'a', 'b', 'c' }.
I want letters to be sorted together regardless of case, e.g. 'A' next to
'a'.
Why doesn't that case-insensitive sort work? What should I do instead?
CaseInsensitiveComparer operates on strings only, not characters. I don't
think the framework has lexicographical comparers for characters out of the box.
The most straightforward way to achieve what you want is to use a
Comparison<Tthat lifts its arguments to strings:
Array.Sort(c, (a, b) =string.Compare(a.ToString(), b.ToString(),
StringComparison.CurrentCultureIgnoreCase));
There may be a more elegant way.
--
J.
Paul E Collins wrote:
char[] c = { 'a', 'b', 'c', 'A', 'B', 'C' };
Array.Sort(c, CaseInsensitiveComparer.Default);
After this, c is ordered { 'A', 'B', 'C', 'a', 'b', 'c' }.
I want letters to be sorted together regardless of case, e.g. 'A' next to
'a'.
Why doesn't that case-insensitive sort work?
Because the CaseInsensetiveComparer compares strings, and if you use it
to compare something different, it will fallback to using
Comparer.Default.Compare() instead.
What should I do instead?
You can make your own comparer that turns the characters to lower case
and compares them:
Array.Sort(c, (x, y) =char.ToLower(x) - char.ToLower(y));
or
Array.Sort(c, delegate(char x, char y) { return char.ToLower(x) -
char.ToLower(y); });
--
Göran Andersson
_____ http://www.guffa.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: its me |
last post by:
Let's say I have a class of people...
Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class
And I declare an array of this class...
|
by: JasBascom |
last post by:
say i had
97456 and
23456
is there already a sort function to check which one begins with the smaller
number rearrange it. in this case the bottom number should clearly be
rearranged to the...
|
by: JasBascom |
last post by:
I thought it was too much to put this program in with my last message. The
program compiles ok, but when I execute, I get access errors.
can someone put it through their compiler please. I think...
|
by: ritchie |
last post by:
Hi all,
I am new to this group and I have question that you may be able to
help me with.
I am trying to learn C but am currently stuck on this.
First of all, I have a function for each sort...
|
by: Foodbank |
last post by:
Hi everyone,
I'm having trouble implementing a supposedly simple Least Significant
Digit first Radix Sort. The book I'm using gave me somewhat of a
template that I touched up (below), but I'm...
|
by: Alex Chudnovsky |
last post by:
I have come across with what appears to be a significant performance bug in
..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same
data. Same data on the same CPU gets sorted a...
|
by: AB |
last post by:
Hello All,
I'm trying to replicate a general purpose sort function (think qsort)
void sort(void *arr, const int num, size_t size,
int (*cmp)(void *a, void *b))
{
int i = 0 ;
int j = 0 ;
|
by: rkk |
last post by:
Hi,
I have written a generic mergesort program which is as below:
---------------------------------------------------------
mergesort.h
-----------------------
void
MergeSort(void...
|
by: kuchma2000 |
last post by:
Hi.
I have a C program (see code below) which I need to urgrade. All I need to do is:
a. Extract the temps for the City #1 and store in a one dimensional array, display the array.
b. Sort the one...
|
by: Randeh |
last post by:
Using Visual Studio 2005, right now my only error (for now) is something with the function prototype that I can't figure out for the life of me. Every data type is unexpected for my function. I'm...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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...
| |