Case-Insensitive Sorting
-
Suppose we have a new student whose name is "mcDonald Jones".
Although we don't know exactly why the letter "m" is written in
lowercase, when we arrange the order of English names, our rule should
be case-insensitive. That is, both uppercase "M" and lowercase "m"
should be in front of "S".
- Re-write the comparison function
( operator>()
or operator<() )
in the previous exercise
so that it will handle the sorting
rule. You don't have to modify anything in the main() function.
Everything is handled by the object itself. This is the advantage of
"encapsulation" in object-oriented programming.
- To make strings case-insensitive, you may simply convert all
characters to uppercase by the function
toupper(), or to lowercase by
the function
tolower().
- If you look at the ASCII
code table, you will notice that the ASCII codes of lowercase 'a'
and uppercase 'A' differs by 32, so does lowercase 'z' and uppercase
'Z'. Therefore, you may convert a character to uppercase by
c &= 0xDF,
or to lowercase by
c |= 0x20.
- By the way, Chinese characters are usually ordered by the number of
strokes. Therefore, if we have another student 文天祥, you may see that
his position is before 柳宗元.
For the input
東坡 蘇
Sandra Bullock
mcDonald Jones
宗元 柳
Julia Roberts
天祥 文
the output may look like
Julia Roberts
mcDonald Jones
Sandra Bullock
文天祥
柳宗元
蘇東坡