SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2004
    Location
    Minneapolis, MN
    Posts
    1,924
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting An Array

    I am having some difficulty in getting an array to properly sort. I figured I would post for help as I've been trying to get this to work for almost an hour. Here is teh code I have now:

    PHP Code:
    function sortRoutines($a$b) {
        
    $a_rating = (int) $a->rating;
        
    $b_rating = (int) $b->rating;
        
        if (
    $a_rating == $b_rating) {
            return 
    0;
        }
        return (
    $a_rating $b_rating) ? -1;

    The array I am comparing is an array full of objects. Each object has a number of members, but I want to sort them based upon their rating.

    The first issue I encountered I realized that the variables were strings, so I fixed that (I think).

    Now it will work the first time I load the page, but when I load it a second time Apache quits on me. I could really just use some help as this has had me stumped for way too long.

  2. #2
    Non-Member Gator99's Avatar
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use can try a quick sort algo, but sort it by object rating value rather than the value of the array, eg:
    Code:
    function quicksort($seq) {
      if(count($seq)>1) {
        $k = $seq[0]->rating; //$seq[0];
        $x = array();
        $y = array();
        for($i=1; $i<count($seq); $i++) {
          if($seq[$i]->rating <= $k) {//$seq[$i]
            $x[] = $seq[$i];
          } else {
            $y[] = $seq[$i];
          }
        }
        $x = quicksort($x);
        $y = quicksort($y);
        return array_merge($x, array($k), $y);
      } else {
        return $seq;
      }
    }
    $sorted=quicksort($my_array);

  3. #3
    SitePoint Wizard
    Join Date
    Jul 2004
    Location
    Minneapolis, MN
    Posts
    1,924
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll give that a shot in a few minutes.

    What stumps me is why Apache just quits out only the second time on it.

  4. #4
    SitePoint Wizard
    Join Date
    Jul 2004
    Location
    Minneapolis, MN
    Posts
    1,924
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That function did not work as well. It has got to be something odd just with array or such? I even tried throwing and catching, which worked the first time, but then shut down on a refresh.

    I'm investigating this further as maybe it's something to do with my server seutp.

    Edit:
    It must be my setup or something. I uploaded the file to my client
    's hosting account and my function worked like a charm. Still puzzles me. :S


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •