# Author: Tommy Emricko (tommy@emricko.com) # Sort list box by DISPLAYED VALUE # Example for use: Making a dropdown list of states, with the state # abbreviation as the value and the state *name* as the displayed value. # If you sorted the hash table values as normal, in the dropdown box, # Massachusetts (MA) would come before Maine (ME), which isn't exactly # right... # Examples $FORM{'state'} = 'MA'; $dropdownstatebox = &getListBoxSort( $FORM{'state'}, ('ME','Maine','PA','Pennsylvania','MA','Massachusetts') ); %states = ('ME','Maine','PA','Pennsylvania','MA','Massachusetts'); $dropdownstatebox = &getListBoxSort( $FORM{'state'}, %states ); $dropdownzipbox = &getListBoxSort( $FORM{'zip'}, ('19390','West Grove','15213','Pittsburgh','19711','Newark') ); sub getListBoxSort($$) { my( $opt ) = shift; my( %list ) = @_; my( $ret ); sub byListDisplay { $list{$a} <=> $list{$b} || $list{$a} cmp $list{$b}; } foreach $x (sort byListDisplay keys( %list )) { my( $foo ) = 'SELECTED' if( $opt eq $x ); $ret .= ""; } return $ret; }