Gossamer Forum
Home : General : Perl Programming :

Tricky Sort

Quote Reply
Tricky Sort
Bonjour,

Im selecting values from a mysql column, for example....

Row1: abc
Row2: cde
Row3: efg
Row4: ABC
Row5: CDE
Row6: CDE
Row7: CDE

What I want to do is sort the values and remove duplicates. I've managed to write the code to select the values, sort and remove duplicates but the way I tried (recommended by perldoc) doesn't seem to want to let me sort without capitals coming first.

Code:
Code:
while (my (@key) = $sth->fetchrow_array) {
push @vals, $key[0];
}

undef %value;
@value{@vals} = ();
@sorted = sort keys %value;

Now that does the job nicely except I end up with:

ABC
CDE
abc
cde
efg

...does anyone know how I can sort case insensitively?

It isn't as easy as you think because the following doesn't work either:

@sorted = sort { lc($value{$a}) cmp lc($value{$b}) } keys %value;

Thanks!



Last edited by:

PaulW: Nov 14, 2001, 6:17 PM
Quote Reply
Re: [PaulW] Tricky Sort In reply to
Why not make mysql sort it for you?
SELECT DISTINCT col FROM table ORDER BY col
that should get the distinct (ie. no duplicates) values, and order it case insensitively (not 100% sure about the case insensitively part hehe).



Adrian
Quote Reply
Re: [brewt] Tricky Sort In reply to
I can't do that unfortunately. I guess my example was bad.

Some rows have multiple values so if I used distinct it may skip some values might it?

eg...

Row1 : valueA, valueB
Row2 : valueA, valueC

If I use distinct will it not skip Row2, hence leave me without valueC or do I have that totally wrong?

Last edited by:

PaulW: Nov 14, 2001, 7:18 PM
Quote Reply
Re: [brewt] Tricky Sort In reply to
Hmm using DISTINCT didn't work due to some rows having one value and others having multiple.
Quote Reply
Re: [brewt] Tricky Sort In reply to
Ah don't worry, I decided to forget about case and make everything lowercase and it works fine now. Thanks :)