Sorting Arrays
I do always have hard times with sorting especially with arrays. And this time I want to share what I got after those sorting times. hehehe. Enjoy!
static function sortAscending(&$arr, $key){
$sort_col = array();
foreach ($arr as $sub) $sort_col[] = $sub[$key];
if(is_numeric($sort_col[0]))
array_multisort($sort_col, SORT_NUMERIC, $arr);
else if(is_string($sort_col[0])){
$sort_col = array_map(’strtolower’, $sort_col);
array_multisort($sort_col, SORT_STRING, $arr);
}
}
static function sortDescending(&$arr, $key){
$sort_col = array();
foreach ($arr as $sub) $sort_col[] = $sub[$key];
if(is_numeric($sort_col[0]))
array_multisort($sort_col, SORT_NUMERIC, SORT_DESC, $arr);
else if(is_string($sort_col[0])){
$sort_col = array_map(’strtolower’, $sort_col);
array_multisort($sort_col, SORT_STRING, SORT_DESC, $arr);
}
}
For more codes, just leave a comment.
- Expressions, Software | Time: 12:04 am



Lahe lge na unsa na nga language . . ?(Patrick eat’s his hair off)
Comment by Patrick — January 29, 2008 @ 7:36 am
Thanks for dropping by Pat. It’s a PHP language…hehehe…it works with sorting a list of data..
Hope to hear you more! Thanks.
Comment by Administrator — January 29, 2008 @ 11:16 am