Gossamer Forum
Home : Products : DBMan : Customization :

Alpha/numerical sorting

Quote Reply
Alpha/numerical sorting
I'm trying to create a sort function that permits me to sort this my Status field by status as opposed to alpha. I need a sorting ruitine that will change:

the ordering to Leader = 1; Officer = 2; Member = 3. Keep in mind I'm pretty new to cgi but somewhat knowledgeable on html.

My .cfg file fields for this field look like this:

%db_select_fields = (
'title' => 'Leader,Officer,Member',
);

my record layout for this field for viewing in .pl is:

<td width="80"$fsmall><br><a href="$db_script_link_url&&view_records=1&key=*&sb=1&so=ascend">Title</a></b></font></td>

I need to figure out two things:

(1) What new function/programming needs to be added to sort via number and

(2) Where that new function/programming needs to be added.

Any help would be greatly appreciated. FYI, I read the sorting stuff on the FAQ by DBJeni but I couldn't find anything on point with the exception of a post discussing <Input type blahblahblah> but it ended abruptly...
Quote Reply
Re: [vigdis] Alpha/numerical sorting In reply to
What I would do is create a new field in your database and have its contents decided by the value of the field "title".

So if Leader is picked the new field lets call it "title_rank" would be filled with 1, if Officer then fill it with 2, etc.

Then when you come to view just sort by the "title_rank" field.

Heres some simple code that will do it, place it in db.cgi, sub add_record and sub modify_record after the my ($output, $status, $counter); bit
Code:
if ($in{'title'} eq 'Leader') { $in{'title_rank'} = '1';}
elsif ($in{'title'} eq 'Officer') { $in{'title_rank'} = '2';}
elsif ($in{'title'} eq 'member') { $in{'title_rank'} = '3';}

otherwise you could use a hash refferencing through it for the name value pairs..

chmod
Quote Reply
Re: [chmod] Alpha/numerical sorting In reply to
What I do in such cases is to simply add a digit before the options: "1Leader, 2Member, 3Whatever".

You can then do a normal alpha sort (as long as you don't have 10 or more options!), and simply cut off the initial digit for printing.

if ($rec{'fieldname'} =~ /(\d)(.*?)/) { $rec{'fieldname'} = $2;}

print qq|$rec{'fieldname'}|;
kellner
Quote Reply
Re: [vigdis] Alpha/numerical sorting In reply to
Thanks guys for the input. I am working with Vigdis on this and it was driving us crazy on how to do this so our rank structure could be view as it should (Top down.)