#!/usr/bin/perl -wT use strict; # position to sort by my $sortbyfield = 3; my @input = ( '1|2|1|4|5|6|7|8|9', '1|2|2|5|5|6|7|8|9', '1|2|0|1|5|6|7|8|9', '1|2|9|2|5|6|7|8|9', '1|2|8|3|5|6|7|8|9', '1|2|7|4|5|6|7|8|9', '1|2|6|7|5|6|7|8|9', ); # Schwartzian Transform my @output = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, getfieldvalue($_, $sortbyfield)] } @input; print join( "\n", @output), "\n"; sub getfieldvalue { my ($line, $fieldno) = @_; my @items = split /\|/, $line; return $items[$fieldno]; } __END__ returns (sorted by 3rd _position, arrays start at 0) 1|2|0|1|5|6|7|8|9 1|2|9|2|5|6|7|8|9 1|2|8|3|5|6|7|8|9 1|2|1|4|5|6|7|8|9 1|2|7|4|5|6|7|8|9 1|2|2|5|5|6|7|8|9 1|2|6|7|5|6|7|8|9