32 users online (1 members and 31 guests)  


  Results 1 to 2 of 2

Related

  1. Top 10 sorting    Forum: Javascript Forum
    Replies: 11
  2. Sorting by column on webpage    Forum: CGI Perl Forum
    Replies: 2
  3. Time sorting    Forum: Javascript Forum
    Replies: 1
  4. Sorting with perl    Forum: CGI Perl Forum
    Replies: 2
  5. Help, i need help with .htc files..    Forum: CSS Forum
    Replies: 3
  1. #1
    blink's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2005
    Posts
    19

    Sorting alhpanumeric files

    Hello all,

    I have a following script to list all files in a given directory:

    my @files_found = `ls *`;
    my @sorted_files_found = sort {$a <=> $b} @files_found;

    The directory has following files:

    image1.jpg
    image2.jpg
    image10.jpg
    image20.jpg

    The sorted outcome is like this:

    image1.jpg
    image10.jpg
    image2.jpg
    image20.jpg

    How can I sort the array of filenames correctly?

  2. #2
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: Sorting alhpanumeric files

    try something like this:
    Code:
    my @files_found = `ls *`;
    my @sorted_files_found = sort {substr($a, index($a, 'image')) <=> substr($b, index($b, 'image'))} @files_found;
    i have no idea if this will work, and it is a pain to test it, so if it doesnt do what you are looking for, just reply, and i will see about re-doing it.

    -ALL



Tags for this Thread