NAME

sort_elements


AUTHOR

copyright (c) by Jim Turner 2000, 2001, 2002, 2003, 2004.


EXAMPLES

require 'sort_elements.pl'; @names = ('Doe, John', 'Smith, Richard', 'Jones, Mike'); @phones = ('111-1111', '222-2222', '333-3333'); @ages = (50, 26, 41); @l = &sort_elements(\@names, \@phones, \@ages);

This sorts and rearranges each array based on the 1st one and sets @l to 0, 2, 1; @names to ``Doe, John'', ``Jones, Mike'', ``Smith, Richard''; and @ages to 50, 41, 26

@l = &sort_elements(1, \@names, \@phones, \@ages);

This sorts only elements [1..] of each array (leaving the zeroth element of each array in it's position and sets @l to 0, 2, 1; @names to ``Doe, John'', ``Jones, Mike'', ``Smith, Richard''; and @ages to 50, 41, 26

@l = &sort_elements(0, 1, \@names, \@phones, \@ages);

This sorts only the 1st 2 elements of each array (leaving any remaining elements (the third) of each array in it's position) and sets @l to 0, 2, 1; @names to ``Doe, John'', ``Jones, Mike'', ``Smith, Richard''; and @ages to 50, 41, 26

@l = &sort_elements_by_list(2, [], \@names, \@phones, \@ages);

This sorts all elements based on the 3rd array (``@ages'') (zero would mean the 1st array) and sets @l to 1, 2, 0; @names to ``Smith, Richard'', ``Jones, Mike'', ``Doe, John''; and @ages to 26, 41, 50.

@l = &sort_elements_by_list([-3, 1], [], \@names, \@phones, \@ages);

This sorts by ``@ages'' descending, then ``@names'' ascending and sets @l to 0, 2, 1; @names to ``Doe, John'', ``Jones, Mike'', ``Smith, Richard''; and @ages to 50, 41, 26

@l = &sort_elements_by_list(3, [undef, undef, 3], \@names, \@phones, \@ages);

This sorts by ``@ages'', using a ``%3d'' (numeric integer) format for sorting the 3rd array (``@ages''). The other two arrays are sorted alphabetically. The elements of the format array (2nd argument) can be integers (which for any integet ``i'' will be converted to the printf string ``%id''), decimal numbers (which for any value ``i.j'' will be converted to the printf string ``%i.jf''), or standard printf strings.

The ``sort_elements()'' function is simply a shortcut for: &sort_elements_by_list([], [], @_);


DESCRIPTION

sort_elements.pl

copyright (c) by Jim Turner 2000, 2001, 2002, 2003, 2004.

Permits sorting of multiple arrays at once based on a single array. ie. if you had 3 arrays: one of names, another of addresses, and another of phone#s, you can sort all three by name and maintain the correct indices of the other arrays.