Last active
February 17, 2017 03:25
-
-
Save jesse1981/5418412 to your computer and use it in GitHub Desktop.
Function to sort an array, in this example, used in conjunction with getCustomTypeData.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment