<?php  if (!defined('HTTP_SERVER')) die('You can not access this file directly!');
/**
  @licence GPL 2005-2010  The osCSS developers - osCSS Open Source E-commerce
  @portion code http://www.php.net/manual/fr/function.uasort.php
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.0.9
  @date  15/08/10, 13:06
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8

$stock = array(array("id" => 1,"item" => "hat",
                 "number" => 5,    "price" => 15),
                array("id" => 5,"item" => "shoes",
                 "number" => 1,    "price" => 50),
                array("id" => 3,"item" => "tie",
                 "number" => 2,    "price" => 30));

$sort = new multiSort();

//will sort the array by id in ascending order
print_r ($sort->sortArray($stock,'id','desc'));

//will sort the array by id in descending order
print_r ($sort->sortArray($stock,'id','asc'));

//can also be used like so (specifying the index of the array)
print_r ($sort->sortArray($stock,'0','desc'));
*/
class multiSort {

  public function __construct(){
  }

  public function sortArray($array,$parameter,$_function='asc') { return $this->_uasort($array,$_function,$parameter); }

  private function asc ($a, $b, $p) {
    $resa=(is_array($a))? $a[$p] : $a->$p;
    $resb=(is_array($b))? $b[$p] : $b->$p;
    return (strcmp ($resa,$resb));
  }

  private function desc ($a, $b, $p) {
    $resa=(is_array($a))? $a[$p] : $a->$p;
    $resb=(is_array($b))? $b[$p] : $b->$p;
    return -1 * (strcmp ($resa,$resb));
    }

  private function _uasort($array,$func,$param) {
    for($i=0;$i<sizeof($array);$i++) {
      $tmp = $i;
      for($j=1;$j<sizeof($array);$j++) {
	$result = $this->$func($array[$tmp],$array[$j],$param);
	if($result == -1) {
	  $array = $this->arraySwap($array,$tmp,$j);
	  $tmp = $j;
	}
      }
    }
    return $array;
  }

  private function arrayswap($arr, $src, $dst) {
    $tmp = $arr[$dst];
    $arr[$dst] = $arr[$src];
    $arr[$src] = $tmp;
    return $arr;
  }
}
?>