jesse1981
4/19/2013 - 5:59 AM

Function to sort an array, in this example, used in conjunction with getCustomTypeData.

Function to sort an array, in this example, used in conjunction with getCustomTypeData.

function sortCustom($data,$field,$datatype="text",$direction="desc") {
    $cycle = false;
    for ($i=0;$i<(count($data)-1);$i++) {
        switch($datatype) {
            case "date":
                $arrDate1 = explode("-", $data[$i][$field]);
                $mkDate1 = mktime(1,1,1,$arrDate1[1],$arrDate1[2],$arrDate1[0]);
                $arrDate2 = explode("-", $data[$i+1][$field]);
                $mkDate2 = mktime(1,1,1,$arrDate2[1],$arrDate2[2],$arrDate2[0]);

                
                $cycle = (($direction=="desc") && ($mkDate1<$mkDate2)) ? true:false;
                if (!$cycle) {
                    $cycle = (($direction=="asce") && ($mkDate1>$mkDate2)) ? true:false;
                }
                break;
            case "text":
                
                $cycle = (($direction=="desc") && (strcmp($data[$i][$field], $data[$i+1][$field])<0)) ? true:false;
                if (!$cycle) {
                    $cycle = (($direction=="asce") && (strcmp($data[$i][$field], $data[$i+1][$field])>0)) ? true:false;
                }
                break;
           case "number":
               
                $cycle = (($direction=="desc") && ((int)$data[$i][$field]<(int)$data[$i+1][$field])) ? true:false;
                if (!$cycle) {
                    $cycle = (($direction=="asce") && ((int)$data[$i][$field]>(int)$data[$i+1][$field])) ? true:false;
                }
               break;
           default:
               // end
               break;
        
        }
        if ($cycle) {
            $tempArray = $data[$i+1];
            $data[$i+1] = $data[$i];
            $data[$i] = $tempArray;
            
            $data = sortCustom($data, $field,$datatype,$direction);
        }
    }
    return $data;
}