發表文章

目前顯示的是 2015的文章

[javascript] 二維陣列的排序

陣列格式 var a = [ {name:'hdj', filename:20140930174726.jpg}, {name:'yim', filename:1_title.jpg}, {name:'hdq', filename:20150930174726.jpg}, {name:'rrr', filename:3_title.jpg}, {name:'ddd', filename:2_title.jpg}, ]; 希望根據 filename 來進行排序,可以使用底下的 function function listSortBy ( arr , field , order ){     var refer = [], newrefer = [], comp_arr =[], result =[];     var order = order == ' asc' ? ' asc ' : 'desc ' , index ;     for ( i = 0 ; i < arr . length ; i ++){         // 1_title.jpg 20140930174726.jpg         var delExt = arr [ i ][ field ]. split ( "." );         // 1_title 20140930174726         var name    = delExt [ 0 ]. split ( "_" );         if ( typeof ( name [ 1 ]) == 'undefined' ) {             refer [ delExt [ 0 ]] = i ;             comp_arr [ i ]       = delExt [ 0 ];         } else {             refe

[PHP] 二維陣列的排序

可使用PHP內建的 function array_multisort (適用PHP 4, PHP 5) $data = array( array('name' => 'richard', 'score' => 20), array('name' => 'alex', 'score' => 18), array('name' => 'jack', 'score' => 22), ); 使用方式 $score = array(); foreach ($data as $user) { $score[] = $user['score']; } array_multisort($score, SORT_ASC, $data); 另一種方式使用 function usort (適用PHP 4, PHP 5),使用此 function 就可自訂比較的方式 $output_data= array(2833, '2833A', 2511, 3000); function cmp($a, $b) { if ($a['stockid'] == $b['stockid']) { return 0; } return ($a['stockid'] < $b['stockid']) ? -1 : 1; } usort($output_data, "cmp"); # Output: $output_data= array(2511, 2833, '2833A', 3000);