Hi Everyone,
I have a list of strings whose values come from a fixed set. I need to sort this list in an arbitrary order.
The order of the set is specified by another list of all possible strings, sorted in order in an array.
Here is an example:
my @all_possible_strings_in_order = ('name', 'street', 'city','state', 'postalcode');
my @list_that_needs_to_be_sorted = ('city', 'state', 'name');
I am working in perl. I figure my best bet is to automatically create a hash that associates strings with ordinal numbers, and then sort by reference to those ordinal numbers.
There are about 300 possible strings in the set. Typical lists will have 30 strings that need to be sorted. This isn't going to be called in a tight loop, but it can't be slow either. Automatically building the ordinal hash can't be done ahead of time due to the structure of the program.
I'm open for suggestions on better ways to do this. Thanks!
Edit: You guys are awesome. I can't hold my head up any more tonight, but tomorrow morning I'm going to take the time to really understand your suggestions... It's about time I became proficient with map() and grep().