
16-10-2008, 12:04
|
|
|
|
חבר מתאריך: 05.03.07
הודעות: 243
|
|
מסתבר שזאת לא משימה קלה במיוחד הנה הקוד שמבצע את זה
קוד:
static function sort2d_asc(&$arr, $key){
//we loop through the array and fetch a value that we store in tmp
for($i = 0; $i < count($arr); $i++){
$tmp = $arr[$i];
$pos = false;
//we check if the key we want to sort by is a string
$str = is_numeric($tmp[$key]);
if(!$str){
//we loop the array again to compare against the temp value we have
for($j = $i; $j < count($arr); $j++){
if(StringManip::is_date($tmp[$key])){
if(StringManip::compareDates($arr[$j][$key], $tmp[$key], $type = 'asc')){
$tmp = $arr[$j];
$pos = $j;
}
//we string compare, if the string is "smaller" it will be assigned to the temp value
}else if(strcasecmp($arr[$j][$key], $tmp[$key]) < 0){
$tmp = $arr[$j];
$pos = $j;
}
}
}else{
for($j = $i; $j < count($arr); $j++){
if($arr[$j][$key] < $tmp[$key]){
$tmp = $arr[$j];
$pos = $j;
}
}
}
if($pos !== false){
$arr[$pos] = $arr[$i];
$arr[$i] = $tmp;
}
}
}
|