SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting function, need to be more efficient.

    Wonder if anyone can help. I have an array of arrays (think a big resultset from a mysql query) and I am trying to order them. Basically, trying to replicate mysql ORDER BY.

    So I have:

    $array = array(
    array('name' => 'x', 'timestamp' => 1232343),
    array('name' => 'y', 'timestamp' => 1453456);
    );

    I want to sort the array based on timestamp (which is numeric and always would be).

    I have a function to do this (below). The problem is I am trying to sort large data sets, up to 100,000 rows or so and this function takes many minutes to run, which istn't good enough.

    So, any ideas for a more efficient version?

    PHP Code:
    function array_sort_deep($array) {
       
    // for all arguments without the first starting at end of list
       
    for ($i=func_num_args();$i>1;$i--) {
           
    // get column to sort by
           
    $sort_by func_get_arg($i-1);
           
    // clear arrays
           
    $new_array = array();
           
    $temporary_array = array();
           
    // walk through original array
           
    foreach($array as $original_key => $original_value) {
               
    // and save only values
               
    $temporary_array[] = $original_value[$sort_by];
           }
           
    // sort array on values
           
    sort($temporary_array);
           
    // delete double values
           
    $temporary_array array_unique($temporary_array);
           
    // walk through temporary array
           
    foreach($temporary_array as $temporary_value) {
               
    // walk through original array
               
    foreach($array as $original_key => $original_value) {
                   
    // and search for entries having the right value
                   
    if($temporary_value == $original_value[$sort_by]) {
                       
    // save in new array
                       
    $new_array[$original_key] = $original_value;
                   }
               }
           }
           
    // update original array
           
    $array $new_array;
       }
       return 
    $array;


  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you ever looked @ the usort function ?

    Documentation @ :http://us2.php.net/manual/en/function.usort.php
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Yes I tried that - it is also slow when I used this function:

    PHP Code:
     function cmp ($a$b
     {   
      global 
    $w_o
      if (
    $a[$w_o] == $b[$w_o]) return 0
      return (
    $a[$w_o] < $b[$w_o]) ? -1
     } 
    where $w_o is the index you want to sort on.

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have found a way to get round this by sorting later, so it istn't such a big problem, an efficient solution would be interesting though.

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Have you tried array_multisort which is for sorting multidimensional arrays?

    I would hope it's implemented with an efficient algorithm, but you can always do that yourself. If these rows come from the database partially sorted or with groups of sorted rows, you'd get good average case performance from quicksort. With an in-place partition, you'd only need log(n) extra memory during the sort. Worst case, which is rare with quicksort (why the average case matters), you'd get the same O(n^2) performance you have now, but use less memory.


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
  •