Gossamer Forum
Home : Products : DBMan : Customization :

sorting

Quote Reply
sorting
Hi folks,
I am sure I've seen this before long ago but I cant find it back by searching.

How do I sort a list like:
Aa
aa
B
b

instead of:
A
Z
a
z

(so case sensitive is ignored?)

The code I am using now is:
---------------------------
for ($i = 0; $i <= $#db_cols; $i++) {
#### In the line below, replace 'Category' with the name of your field.
if ($db_cols[$i] eq "Userid" ) {
$fieldnum = $i; $found = 1;
last;
}
}
if ($found) {
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
++$count1{$fields[$fieldnum]};
}
close DB;
foreach $option (sort keys %count1) {
------------------------
which returns:
A-Z
and then
a-z



Rock & Roll

Close Watch
Quote Reply
Re: sorting In reply to
Change

foreach $option (sort keys %count1) {

to

foreach $option (sort {lc{$a} cmp lc{$b}} keys %count1) {



JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: sorting In reply to
JP,

This results in:
----------
SongStud
Justice
sweetie
mwolverine
santo2000
sittinlow2g
solidJ
Fluffy9
debrakay
PoohBear3
DreamingStar
katrie
MrMusic
cian
....ETC...
-----------

Close Watch
Quote Reply
Re: sorting In reply to
When I gave you the code, I tested it out and it seemed to work fine. But I tested it with your names and it didn't work at all.

I have a workaround that is very clumsy, but it will work.

Code:

foreach $unsort (keys %count1) {
push (@option_list,ucfirst($unsort));
}
foreach $sort (sort @option_list) {
if (exists $count1{$sort}) {
push (@sorted,$sort);
}
else {
push (@sorted, lcfirst($sort));
}
}
foreach $option (@sorted) {
What this does is stick each of the usernames into an array and changes the first letter to upper case. Then it sorts the uppercase usernames. If there is a username that matches the % count1 hash, it puts it into a second array. If not, it changes the first character back to lowercase and then puts it into the second array. Then it uses the second array to print out the usernames.

Extremely clumsy, but I don't know how else to do it.


JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: sorting In reply to
Hi JP,
Output is now:
--------------------
AKEAL 6 lyric(s)
ASARO 1 lyric(s)
absolut 29 lyric(s)
adzo 1 lyric(s)
ahphope 2 lyric(s)
Aien 3 lyric(s)
Akeal 5 lyric(s)
amdramulus 3 lyric(s)
Amnotamuse 8 lyric(s)
AngelM 13 lyric(s)
Angelphisy 12 lyric(s)
angelsmist 1 lyric(s)
-------------------------
(shouldnt the red names be under eachother?)

It's much better than I had in the first place and I dont care if you call it clumsy, it's works great and thank you !


Rock & Roll

Close Watch
Quote Reply
Re: sorting In reply to
Okay. Let's try something else

Code:

foreach $unsort (keys %count1) {
push (@option_list,uc($unsort));
}
foreach $sort (sort @option_list) {
foreach $hash (keys %count1) {
if ($sort =~ /$hash/i) {
push(@sorted,$hash);
}
}
}
foreach $option (@sorted) {
This is even clumsier, but it should give you what you want.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: sorting In reply to
Hi JD,
All looks great, only one strange thing (out of 196 names)
-----------
absolut 29 lyric(s)
adzo 1 lyric(s)
ahphope 2 lyric(s)
Aien 3 lyric(s)
AKEAL 6 lyric(s)
Akeal 5 lyric(s)
AKEAL 6 lyric(s)
Akeal 5 lyric(s)

Amdramulus 3 lyric(s)
amnotamuse 8 lyric(s)
AngelM
-----------
This name get returned twice, (when clicked on it. shows 11 records/result of Akeal + AKEAL)
URL: http://www.12inter.net/...lt&author_list=1



Rock & Roll

Close Watch
Amsterdam, The Netherlands
Quote Reply
Re: sorting In reply to
I can see why it happens. You really shouldn't have two usernames where the only difference is the case. But you do. Smile

Hang on. I'll see if I can figure out a fix for this.

Okay. This is *really* clumsy!

Change

Code:

if ($sort =~ /$hash/i) {
push(@sorted,$hash);
}
to

Code:

if ($sort =~ /$hash/i) {
unless ($used{$hash}) {
push(@sorted,$hash);
$used{$hash} = 1;
}
}

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: sorting In reply to
You call it clumsy
I call it BRILLIANT !

Thank you so much.
JP for president !

(Eh, but how could I prevent ppl signing up with AB AND ab ?)

Rock & Roll

Close Watch
Amsterdam, The Netherlands
Quote Reply
Re: sorting In reply to
I'm pretty sure it will work if you change

Code:

while (<PASS>) {
/^\Q$in{'userid'}\E:/ and ($message = "userid already exists. Please try another.");
}
to

Code:

while (<PASS>) {
/^\Q$in{'userid'}\E:/i and ($message = "userid already exists. Please try another.");
}
The i means "ignore case."

In Reply To:
JP for president !
Oh, no! Not that!!!!! Laugh

You're welcome. I'm glad I could help.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: sorting In reply to
:)
thanx again (and again)

Rock & Roll

Close Watch
Amsterdam, The Netherlands