sub prepare_sort_menu
{
 # Declare our variables as local to this subroutine:
 my (%data,$sortdata,$opts);
 
 # Get a 2D hash of data, from which we get field names:
 %data = %{get_data()};
 
 # Get the template file for the "Sort Entries" menu:
 $sortdata = getfile($sort_template,1); 
 
 
 # Loop over each field name from the first entry:
 
 # a. Create a down down menu option for the field name.
 # b. Add the option into a list of options for use ($opts).
 
 foreach my $key (sort keys %{$data{0}}){
  $opts .= qq(<option value="$key">@{[ucfirst($key)]}</option>\n);
 }
 
 
 # Replace a couple of special content tags with their content:
 
 # a. Replace the "sort options" tag with the list of options.
 # b. Replace the current page tag with the number of the current page (1 if none specified).
 # c. Feed the template data through expand_vars() to handle other content tags.
 
 $sortdata =~ s/\Q%%SORT_OPTS%%\E/$opts/g;
 $sortdata =~ s/\Q%%PAGE%%\E/param("page")||1/ge;
 $sortdata = expand_vars($sortdata);
 
 # Return the template data with correct info:
 return $sortdata;
}
1;