-1

Struggling a little to deal with sorting a table by column name. The issues are the data is encrypted so cant just do a sort by the column in the direction needed (ascending descending). So i thought I can use usort but although i can call a function with it i.e. usort( $data, "sortMyData" ); I can't pass the field to sort or it's direction into it. So to do this I would need to write a function to sort for each possible column which is not ideal at all, does anyone know a way i could for instance add another parameter to usort which would contain its property to sort by and it's direction.

Perhaps an alternative would be to decrypt the entire set of data into some memory table but as the data is encrypted and secure i wonder if that would open the way for a vulnerability!

My last option would be to build this into a foreach loop which i could see would make sense but is this the only way?

thanks

Craig

ThurstonLevi
  • 664
  • 13
  • 34

2 Answers2

0

Refer this stackoverflow question, Pass extra parameters to usort callback

it provides an example of passing extra parameters to usrot function.

function sort_by_term_meta( $terms, $meta ) 
{
    usort($terms, array(new TermMetaCmpClosure($meta), "call"));
}

function term_meta_cmp( $a, $b, $meta )
{
    $name_a = get_term_meta($a->term_id, $meta, true);
    $name_b = get_term_meta($b->term_id, $meta, true);
    return strcmp($name_a, $name_b); 
} 

class TermMetaCmpClosure
{
    private $meta;

    function __construct( $meta ) {
        $this->meta = $meta;
    }

    function call( $a, $b ) {
        return term_meta_cmp($a, $b, $this->meta);
    }
}

basically you need to create a class function to do the sorting and you can pass additional parameters (column, direction) when constructing the class.

Prabath Perera
  • 240
  • 2
  • 9
  • 1
    Rather unnecessarily complex, with the `use` keyword this can be done without creating an extra class, see this answer further down in the same question thread: https://stackoverflow.com/a/22610655/10955263 – 04FS Jan 16 '20 at 10:35
0

I can't pass the field to sort or it's direction into it.

Actually, you can. There is an example:

<?php
  // Example data
  $data = array(
    array('name' => 'Gamma',  'val' => 25),
    array('name' => 'Beta',  'val' => 5),
    array('name' => 'Alpha', 'val' => 10)
  );


  function sortme(&$array, $onfield, $isdesc) {
     usort($array, 
           function($a, $b) use ($onfield, $isdesc) { // 'use' keyword allows to reference external variables from the inside
              // custom method to obtain and comapre data;
              $v1 = isset($a[$onfield]) ? $a[$onfield] : NULL; 
              $v2 = isset($b[$onfield]) ? $b[$onfield] : NULL;

              if ($v1 < $v2) return ($isdesc ? 1 : -1);
              elseif ($v1 > $v2) return ($isdesc ? -1 : 1);
              else return 0; 
              // Note: the conditions above can be replaced by spaceship operator in PHP 7+:
              // return $isdesc ? ($v2 <=> $v1) : ($v1 <=> $v2) ;
           }
          );
  }


  sortme($data, 'name', false); // sort by `name` ascending
  print_r($data); // Alpha -> Beta -> Gamma

  sortme($data, 'val', true); // sort by `val` descending
  print_r($data); // 25 -> 10 -> 5
AterLux
  • 4,566
  • 2
  • 10
  • 13