Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Sorting a loop by date

    Hi,

    I'm using Simple XML to read from an XML file. I've done a loop for each event. It gives me all the events back in the order of the XML file. However, instead I am trying to sort events by date. Any idea how I could do this?

    Please see my code below. FYI, the date and time is in UTC timestamp format (eg. 1377361800)

    Thanks in advance for your help.

    PHP Code:
    $newresult $result->xpath("//match/matchname[.='$uniqueteam']/..");


       foreach (
    $newresult as $newmatchlist) {
        


    $newmatch trim($newmatchlist->{'matchname'}[0]);
    $sport $newmatchlist->{'sportname'}[0];
    $tournament $newmatchlist->{'tournamentname'}[0];
    $time=$newmatchlist->{'matchname'}[0]['dateandtime']; 


    Last edited by denhamd2; 08-19-2013 at 09:55 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    The only way to do this is to store the data you want into an array or object, then sort that. My usual method is to store the data into an array (in this case, you could easily do an array of objects, so no need to convert data), using the column I want to sort by as the key (timestamp in this case, if you can be assured its unique). If what I want to is not assured unique, I'll usually create a second array with the same keys as the data array. Then sort by key for the first method, or a regular sort for the second, both with key preservation, and then loops again to display.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Its not the *only* way to do it, but it sure is the most pleasant way to do it (PHP wise). IMO the cost of an additional loop and memory is well worth the ease of operation.
    This is a simple method of collecting your items from your first loop, then sorting, than iterating again to output.
    PHP Code:
    function sortByDateTime($a$b)
    {
        return (isset(
    $a->matchname[0]['dateandtime'], $b->matchname[0]['dateandtime']) ? ($a->matchname[0]['dateandtime'] - $b->matchname[0]['dateandtime']) : 0);
    }
    $aItems = array();
    foreach (
    $newresult as $newmatchlist)
    {
        
    $aItems[] = $newmatchlist;
    }

    usort($aItems'sortByDateTime'); 
    Then iterate the $aItems. Untested, but works alright upstairs.

    Alternatively, since you are using XPATH you may want to consider the use of the xpath sort. I don't think I've ever used this before myself, but it looks like it would simply take your node, and then sort it based on a pattern provided (such as the path to another node relative). If you have the proper versions, I'd suggest that would work out perfectly.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Fou-Lu View Post
    Its not the *only* way to do it
    Yah, sorry, bad turn of phrase on my part. I meant the only way I know how to do it.


  •  

    Posting Permissions

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