foreach ( sort gs::long_strings_first(), @$raw_hash_keys_ar ) { #do_stuff; } package gs; sub long_strings_first { return ( 0 ) unless defined $b; length $b <=> length $a || lc $b cmp lc $a } #### 'All-American Residential Framing' 'Builders Assistant' 'Colson Plumbing' 'Electric Systems' 'Greenways' 'P & L Lighting' 'Regis Co.' 'Robert E. Matthews' #### foreach ( sort { length $b <=> length $a || lc $b cmp lc $a } @$raw_hash_keys_ar ) { # do stuff; } # Processed as: 'All-American Residential Framing' 'Robert E. Matthews' 'Builders Assistant' 'Electric Systems' 'Colson Plumbing' 'P & L Lighting' 'Regis Co.' 'Greenways'