sorting numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Davide

    sorting numbers

    Hi all..

    I'm trying to sorting a file in a directory.
    It works well but it sorts me the number in this way:

    1
    10
    11
    2
    20
    21
    ....

    is there a way to sort in this way?
    1
    2
    3
    ...
    10
    11
    12

    this is my code:

    <?php
    if ($handle = opendir('/home/davide/')) {
    $files = array();
    while (false !== ($file = readdir($handle ))) {
    if (($file != "." && $file != "..") AND (eregi($select, $file)))
    $files[] = $file;
    }
    closedir($handl e);
    sort($files);
    foreach($files as $file) {
    $ext = substr(strrchr( $file, "."), 1);
    if ($ext == 'txt') {
    $idx = str_replace('.t xt', '', $file);
    echo "$idx";
    }
    }
    }
    }
    ?>

  • Rik

    #2
    Re: sorting numbers

    Davide wrote:
    Hi all..
    >
    I'm trying to sorting a file in a directory.
    It works well but it sorts me the number in this way:
    >
    1
    10
    11
    2
    20
    21
    ...
    >
    is there a way to sort in this way?
    1
    2
    3
    ..
    10
    11
    12
    >
    this is my code:
    >
    <?php
    if ($handle = opendir('/home/davide/')) {
    $files = array();
    while (false !== ($file = readdir($handle ))) {
    if (($file != "." && $file != "..") AND
    (eregi($select, $file))) $files[] = $file;
    }
    closedir($handl e);
    sort($files);
    Change to natsort($files) ;

    Grtz,
    --
    Rik Wasmus


    Comment

    • Davide

      #3
      Re: sorting numbers

      GREAT!!!
      Thanx!

      Rik ha scritto:
      Change to natsort($files) ;
      >
      Grtz,
      --
      Rik Wasmus

      Comment

      Working...