Sort::PolySort -- general rules-based sorting of lists use Sort::PolySort; $s=new Sort::PolySort; # defaults to 'name' @people=('John Doe','Jane Doll','John Quasimodo Doe'); print join(", ",$s->sort(@people),"\n"; use Sort::PolySort; $s=new Sort::PolySort('dateus'); # sets internal @dates=$s->sort(@dates); # uses internal $s->by('email'); # sets internal @names=$s->sortby('name',@names); # overrides internal @emails=$s->sort(@emails); # internal is still 'email' This module provides methods to sort a list of strings based on parsing the strings according to a configurable set of specifications.

METHODS
The following methods are available: Creates a new polysort object. Takes optional arguement of initial named spec set (defaults to name). Configures for the named set of sorting specs. Arguement is name of spec set. Sorts by the previously-set (by new or by) specs. Arguement is a list of items to sort. Returns the sorted list, leaving the original unchanged. Sorts by the given (named) set of specs. The specs are only changed for this particular sort, so future calls to sort will use whatever specs were in effect before the sortby call. First arguement is name of spec set, second arguement is a list of items to sort. Returns the sorted list, leaving the original unchanged. Returns an associative array of the current sort specs. See the NOTES entry elsewhere in this document. Sets the current sort specs to the given associative array. Specs not appearing in the passed array retain their previous values, so this method can be used along with get to keep state during a subroutine call or to alter particular specs to get new sorting results. Arguement is an associative array. See the NOTES entry elsewhere in this document.

Specs
The following specifications are local to each Sort::PolySort object: Lump last two levels together? Number of levels to consider (0=all) Count fields right to left? Do numerical sort? Do case-sensitive sort? Primary element delimiter (must not be null). Secondary element delimiter (can be null).

Parsing Scheme
The following order is followed to determine the keys used to sort the given array: DELIM2 (if given) Remove up to leftmost (rightmost if R2L is true) occurance of DELIM2 (will be brought back later). DELIM1 Split remainder at all occurances of DELIM1. GLOBTOP (if true) Rejoin left (right if R2L is true) 2 elements (always joined left-to-right, regardless of R2L). R2L (if true) Reverse list of elements. LEVELS Store first LEVELS (all if =0) elements (last 2 considered as a single element if GLOBTOP is true). DELIM2 (if true) Store string from left of DELIM2 as next element. LEVELS (unless 0) Rejoin remaining elements (in original order, regardless of R2L) and store as next element.

Named Specs
The following (case-sensitive) names are used by new, by and sortby to represent pre-defined sets of specs: by European (dd/mm/yy) date by US-style (mm/dd/yy) date by user for each machine (all parts of FQDN) by user for each top-level domain (last 2 atoms) by user for each domain-name (next-to-last atom) by numerical (aaa.bbb.ccc.ddd) ip address by last name/first name/middle name or initials The following errors may occur: by or sortby wasn't passed a specname. new, by, or sortby was passed a name that was not in the list of known specnames. The whole parsing method is pretty perverse, but honestly was the first thing that came to mind. It works, but is not very fast or extensible. Enough interested folks mailed me that I wanted to get this out now, but it's dyin' for a rewrite. So this is just a beta. The main interface will remain the same, but the parser will be rewritten and the spec variables changed. Accessor methods will change as a result (using `%s=$s-'get; ... ;$s->set(%s) will probably still work to save state, though). And accessor methods wll be added so that new names spec sets can be added at runtime or known ones modified. And new named spec sets will be added. And on and on and on... Daniel Macks <dmacks@netspace.org> Copyright (c) 1996 by Daniel Macks. All rights reserved. This module is free software; you may redistribute it and/or modify it under the same terms as Perl itself.