#!/usr/bin/perl use strict; use warnings; my @path = qw( /abc/def/1/2/3/4/5/6/7/8 /abc /abc/1/2/a/b/c /abc/2 /tmp/1/2/a/../b/c /abc/def/1/2/a/b/c abc/def/../../john/Desktop/work abc/../john/Desktop/work ../john/Desktop/work /abc!def/1/2/a/b/c/. /abc/def /abc-def/1/2/a/b /./home ./abcdef/1/2/a/b/c /tmp /usr/a ); foreach (@path) { # Let's get rid of . and .. $_ =~ s!^\./!!; $_ =~ s!^/\./!/!; $_ =~ s!/\.$!/!; $_ =~ s!/\./$!/!; $_ =~ s!/\./!/!g; while ($_ =~ s![^/.]+/[.]{2}/!!) {} # Next, we change every "/" to "\xFF" # and insert the path depth encoded as a # character in front of the path. my $test = substr($_, 1); my $depth = $test =~ tr|/|/|; $_ =~ tr|/|\xff|; $_ = chr($depth) . $_; } @path = sort @path; # Sort list foreach (@path) { $_ = substr($_, 1); $_ =~ tr|\xff|/|; # Undo changes print "\n $_"; # Print sorted list }