sorting an associative array of objects by value?

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

    #1

    sorting an associative array of objects by value?

    Hi all,

    I'm trying to sort an array of objects within an object. Included is a
    dumbed down example so that we can get at the meat of the issue without
    worrying about complexity or validation. Basically, Funk is an object that
    has a name (string) and a value(int). FunkThis represents an object
    containing a list of Funks, with each key corresponding to the name of the
    Funk. I want to sort this list by val, but I get a Warning: usort():
    Invalid comparison function error when I run the script. Keep in mind that
    this is for PHP 4

    I'm open to suggestions to getting this to work.



    <?php

    class Funk {

    var $name;
    var $val;

    function Funk($name,$val ) {
    $this->name = $name;
    $this->val = $val;
    }

    function getVal() {
    return $this->val;
    }

    function getName() {
    return $this->name;
    }
    }

    class FunkThis {

    var $list;

    function FunkThis() {
    $this->list = null;
    }

    function add($funk) {
    $this->list[$funk->getName()] = $funk;
    }

    function compareFunks($a ,$b) {
    $aVal = $a->getVal();
    $bVal = $b->getVal();
    if ($aVal == $bVal) return 0;
    if ($aVal $bVal) return 1;
    if ($aVal < $bVal) return -1;

    }

    function sortByFunk() {
    $list = $this->list;
    $this->list = uasort($list,"c ompareFunks");
    }

    function show() {
    print_r($this->list);
    }
    }

    $a = new Funk("A",3);
    $b = new Funk("B",7);
    $c = new Funk("C",1);
    $d = new Funk("D",9);
    $e = new Funk("E",6);

    $funkthat = new FunkThis();

    $funkthat->add($a);
    $funkthat->add($b);
    $funkthat->add($c);
    $funkthat->add($d);
    $funkthat->add($e);

    $funkthat->sortByFunk() ;

    $funkthat->show();
    ?>

    Greg


  • Michael Fesser

    #2
    Re: sorting an associative array of objects by value?

    ..oO(Greg D)
    >I'm trying to sort an array of objects within an object. Included is a
    >dumbed down example so that we can get at the meat of the issue without
    >worrying about complexity or validation. Basically, Funk is an object that
    >has a name (string) and a value(int). FunkThis represents an object
    >containing a list of Funks, with each key corresponding to the name of the
    >Funk. I want to sort this list by val, but I get a Warning: usort():
    >Invalid comparison function error when I run the script. Keep in mind that
    >this is for PHP 4
    >[...]
    callback
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    | A method of an instantiated object is passed as an array containing an
    | object as the element with index 0 and a method name as the element
    | with index 1.

    Since compareFunks() is a method of FunkThis, you need

    uasort($list, array($this, 'compareFunks') );

    to call it.

    Micha

    Comment

    • Greg D

      #3
      Re: sorting an associative array of objects by value?


      "Michael Fesser" <netizen@gmx.de wrote in message
      news:niiun2tqj9 n2rhhf4meql2e95 oau2vinis@4ax.c om...
      .oO(Greg D)
      >
      >>I'm trying to sort an array of objects within an object. Included is a
      >>dumbed down example so that we can get at the meat of the issue without
      >>worrying about complexity or validation. Basically, Funk is an object
      >>that
      >>has a name (string) and a value(int). FunkThis represents an object
      >>containing a list of Funks, with each key corresponding to the name of the
      >>Funk. I want to sort this list by val, but I get a Warning: usort():
      >>Invalid comparison function error when I run the script. Keep in mind
      >>that
      >>this is for PHP 4
      >>[...]
      >
      callback
      PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

      >
      | A method of an instantiated object is passed as an array containing an
      | object as the element with index 0 and a method name as the element
      | with index 1.
      >
      Since compareFunks() is a method of FunkThis, you need
      >
      uasort($list, array($this, 'compareFunks') );
      >
      to call it.
      Ah, see, I had done that, and when invoking FunkThis->compareFunks() , it
      printed 1.

      I then used uasort($this->list, array($this, 'compareFunks') );

      and it worked fine. Must be because the function parameter is passing the
      array by reference.

      Thanks!

      Greg




      Comment

      • Greg D

        #4
        Re: sorting an associative array of objects by value?


        "Greg D" <gregd@lasmag.c omwrote in message
        news:EZUfh.5656 $a14.2858@newsf e24.lga...
        >
        "Michael Fesser" <netizen@gmx.de wrote in message
        news:niiun2tqj9 n2rhhf4meql2e95 oau2vinis@4ax.c om...
        >.oO(Greg D)
        >>
        >>>I'm trying to sort an array of objects within an object. Included is a
        >>>dumbed down example so that we can get at the meat of the issue without
        >>>worrying about complexity or validation. Basically, Funk is an object
        >>>that
        >>>has a name (string) and a value(int). FunkThis represents an object
        >>>containing a list of Funks, with each key corresponding to the name of
        >>>the
        >>>Funk. I want to sort this list by val, but I get a Warning: usort():
        >>>Invalid comparison function error when I run the script. Keep in mind
        >>>that
        >>>this is for PHP 4
        >>>[...]
        >>
        >callback
        >http://www.php.net/manual/en/languag...types.callback
        >>
        >| A method of an instantiated object is passed as an array containing an
        >| object as the element with index 0 and a method name as the element
        >| with index 1.
        >>
        >Since compareFunks() is a method of FunkThis, you need
        >>
        > uasort($list, array($this, 'compareFunks') );
        >>
        >to call it.
        >
        Ah, see, I had done that, and when invoking FunkThis->compareFunks() , it
        printed 1.
        >
        I then used uasort($this->list, array($this, 'compareFunks') );
        >
        and it worked fine. Must be because the function parameter is passing the
        array by reference.
        >
        Thanks!
        Actually, that's not quite right either (my bad). I didn't read the
        function description fully. uasort affects the array and returns a boolean.
        D'oh!

        This would do it:

        function sortByFunk() {
        uasort($this->list, array($this,"co mpareFunks"));
        }

        and so would this:

        function sortByFunk() {
        $newlist = $this->list;
        uasort($newlist , array($this,"co mpareFunks"));
        $this->list = $newlist;
        }

        What can I say, I'm an idiot. :)

        GregoryD


        Comment

        Working...