How do you sort an array of objects?

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

    How do you sort an array of objects?

    The PHP Manual offered me NO help on this one, so now I appeal to the
    populace:

    [PHP]
    $dirID = @opendir('/my/path/to/files');
    while (($fyl = @readdir($dirID ))) {
    $obj =& new ClassObject($fy l);
    array_push($ima geArray, $obj);
    $obj = null;
    }
    [/PHP]

    I now have an array $imageArray of objects as so:

    Code:
    $imageArray[0]->image_name
    Code:
    $imageArray[0]->image_dimensions
    I need to sort $imageArray by "image_name " or by "image_dimensio ns"
    and I can't think of a way to do it, any ideas?

    Thanx
    Phil
  • Cameron

    #2
    Re: How do you sort an array of objects?

    Phil Powell wrote:[color=blue]
    > The PHP Manual offered me NO help on this one, so now I appeal to the
    > populace:
    >
    > [PHP]
    > $dirID = @opendir('/my/path/to/files');
    > while (($fyl = @readdir($dirID ))) {
    > $obj =& new ClassObject($fy l);
    > array_push($ima geArray, $obj);
    > $obj = null;
    > }
    > [/PHP]
    >
    > I now have an array $imageArray of objects as so:
    >
    >
    Code:
    $imageArray[0]->image_name
    >
    Code:
    $imageArray[0]->image_dimensions
    >
    > I need to sort $imageArray by "image_name " or by "image_dimensio ns"
    > and I can't think of a way to do it, any ideas?
    >
    > Thanx
    > Phil[/color]

    have a look at the user comments in the PHP Manual on the pages about
    array sorting including...



    ~Cameron

    Comment

    • Phil Powell

      #3
      Re: How do you sort an array of objects?

      asort() does not do anything that I would want since I do not want the
      key-val association to be retained. I looked into usort() but that
      failed because the callback function that usort calls never gets
      called...

      $functionBundle Array[0] = get_class($this );
      $functionBundle Array[1] = 'field_name_sor t';
      usort($imageArr ay, $functionBundle Array, $fieldName);

      ....

      function field_name_sort (&$a, &$b, $fieldName) {
      return strcmp(strtolow er($a->$fieldName),
      strtolower($b->$fieldName)) ;
      }

      Problem is that $a and $b need to be compared by index, and I have
      absolutely NO idea how to do that since usort() doesn't allot for
      using any index at all.

      Phil

      Cameron <foo@bar.invali d> wrote in message news:<c00ijv$f0 1$1@news6.svr.p ol.co.uk>...[color=blue]
      > Phil Powell wrote:[color=green]
      > > The PHP Manual offered me NO help on this one, so now I appeal to the
      > > populace:
      > >
      > > [PHP]
      > > $dirID = @opendir('/my/path/to/files');
      > > while (($fyl = @readdir($dirID ))) {
      > > $obj =& new ClassObject($fy l);
      > > array_push($ima geArray, $obj);
      > > $obj = null;
      > > }
      > > [/PHP]
      > >
      > > I now have an array $imageArray of objects as so:
      > >
      > >
      Code:
      $imageArray[0]->image_name
      > >
      Code:
      $imageArray[0]->image_dimensions
      > >
      > > I need to sort $imageArray by "image_name " or by "image_dimensio ns"
      > > and I can't think of a way to do it, any ideas?
      > >
      > > Thanx
      > > Phil[/color]
      >
      > have a look at the user comments in the PHP Manual on the pages about
      > array sorting including...
      >
      > http://uk2.php.net/manual/en/function.asort.php
      >
      > ~Cameron[/color]

      Comment

      • Pedro Graca

        #4
        Re: How do you sort an array of objects?

        Phil Powell wrote:[color=blue]
        > asort() does not do anything that I would want since I do not want the
        > key-val association to be retained. I looked into usort() but that
        > failed because the callback function that usort calls never gets
        > called...
        >
        > $functionBundle Array[0] = get_class($this );
        > $functionBundle Array[1] = 'field_name_sor t';
        > usort($imageArr ay, $functionBundle Array, $fieldName);[/color]
        usort takes /two/ parameters
        Sort an array by values using a user-defined comparison function

        [color=blue]
        > function field_name_sort (&$a, &$b, $fieldName) {
        > return strcmp(strtolow er($a->$fieldName),
        > strtolower($b->$fieldName)) ;
        > }[/color]
        the callback function takes /two/ parameters.
        [color=blue]
        > Problem is that $a and $b need to be compared by index, and I have
        > absolutely NO idea how to do that since usort() doesn't allot for
        > using any index at all.[/color]
        So you need to pass a /third/ parameter to the callback function?

        Use a global variable :-)



        something like this:

        #v+
        <?php
        function user_sort($a, $b) {
        global $GLOBAL_VARIABL E_USED_FOR_USER _SORT;

        // get a better *local* name :-)
        $x = $GLOBAL_VARIABL E_USED_FOR_USER _SORT;
        if ($a->$x == $b->$x) return 0;
        return ($a->$x < $b->$x) ? -1 : 1;
        }

        function show($a, $title='') {
        echo "$title:\n" ;
        foreach ($a as $k=>$v) {
        echo "[$k]";
        foreach ($v as $kk=>$vv) echo " $kk = $vv;";
        echo "\n";
        }
        echo "\n\n";
        }

        $a[0]->fullname = 'Zapotec'; $a[0]->age = 30;
        $a[1]->fullname = 'Michael'; $a[1]->age = 80;
        $a[2]->fullname = 'Abigail'; $a[2]->age = 17;

        show($a);

        $GLOBAL_VARIABL E_USED_FOR_USER _SORT = 'fullname';
        usort($a, 'user_sort');
        show($a, 'sorted by fullname');

        $GLOBAL_VARIABL E_USED_FOR_USER _SORT = 'age';
        usort($a, 'user_sort');
        show($a, 'sorted by age');

        echo "\n\n";
        ?>
        #v-
        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        • Chung Leong

          #5
          Re: How do you sort an array of objects?

          uksort()

          Uzytkownik "Phil Powell" <soazine@erols. com> napisal w wiadomosci
          news:1cdca2a7.0 402061431.1b699 2fc@posting.goo gle.com...[color=blue]
          > asort() does not do anything that I would want since I do not want the
          > key-val association to be retained. I looked into usort() but that
          > failed because the callback function that usort calls never gets
          > called...
          >
          > $functionBundle Array[0] = get_class($this );
          > $functionBundle Array[1] = 'field_name_sor t';
          > usort($imageArr ay, $functionBundle Array, $fieldName);
          >
          > ...
          >
          > function field_name_sort (&$a, &$b, $fieldName) {
          > return strcmp(strtolow er($a->$fieldName),
          > strtolower($b->$fieldName)) ;
          > }
          >
          > Problem is that $a and $b need to be compared by index, and I have
          > absolutely NO idea how to do that since usort() doesn't allot for
          > using any index at all.
          >
          > Phil
          >
          > Cameron <foo@bar.invali d> wrote in message[/color]
          news:<c00ijv$f0 1$1@news6.svr.p ol.co.uk>...[color=blue][color=green]
          > > Phil Powell wrote:[color=darkred]
          > > > The PHP Manual offered me NO help on this one, so now I appeal to the
          > > > populace:
          > > >
          > > > [PHP]
          > > > $dirID = @opendir('/my/path/to/files');
          > > > while (($fyl = @readdir($dirID ))) {
          > > > $obj =& new ClassObject($fy l);
          > > > array_push($ima geArray, $obj);
          > > > $obj = null;
          > > > }
          > > > [/PHP]
          > > >
          > > > I now have an array $imageArray of objects as so:
          > > >
          > > >
          Code:
          $imageArray[0]->image_name
          > > >
          Code:
          $imageArray[0]->image_dimensions
          > > >
          > > > I need to sort $imageArray by "image_name " or by "image_dimensio ns"
          > > > and I can't think of a way to do it, any ideas?
          > > >
          > > > Thanx
          > > > Phil[/color]
          > >
          > > have a look at the user comments in the PHP Manual on the pages about
          > > array sorting including...
          > >
          > > http://uk2.php.net/manual/en/function.asort.php
          > >
          > > ~Cameron[/color][/color]


          Comment

          Working...