Sort::PolySort -- general rules-based sorting of lists |
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.
The following methods are available:
The following specifications are local to each Sort::PolySort object:
GLOBTOP
LEVELS
R2L
NUMERIC
CASE
DELIM1
DELIM2
The following order is followed to determine the keys used to sort the given array:
Remove up to leftmost (rightmost if R2L is true) occurance of DELIM2 (will be brought back later).
Split remainder at all occurances of DELIM1.
Rejoin left (right if R2L is true) 2 elements (always joined left-to-right, regardless of R2L).
Reverse list of elements.
Store first LEVELS (all if =0) elements (last 2 considered as a single element if GLOBTOP is true).
Store string from left of DELIM2 as next element.
Rejoin remaining elements (in original order, regardless of R2L) and store as next element.
The following (case-sensitive) names are used by new, by and sortby to represent pre-defined sets of specs:
The following errors may occur:
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.
Sort::PolySort -- general rules-based sorting of lists |