Diogom Posted January 3, 2013 Share Posted January 3, 2013 (edited) Hello everyone, i want to ask you guys help with a little problem im having sorting an array, it seems simple but i just cant get it to work the way i want. the problem is the following, i have this table: http://img255.images...43/tabelafp.png i want to sort it using the first 2 columns, first by date (LOTE WISE) then by the 'sequential' number (LOTE EXTERNO). It has to be one after the other because the sequential number might not be included sequentially, so its possible to have a newer (higher) LOTE EXTERNO with an older LOTE WISE, also, there is a kind of LOTE EXTERNO which is alphanumeric. the table is inside a multidimensional array in which im sorting this way right now: function cmp($a, $B) { $c = preg_replace('#(\d+)/(\d+)#','${2}/${1}', $a[0]); $d = preg_replace('#(\d+)/(\d+)#','${2}/${1}', $b[0]); return strcasecmp($c, $d); } usort($output['aaData'],"cmp"); $output['aaData']=array_reverse($output['aaData']); $output['aaData'] would be like this "aaData": [["004\/2012","<a href=nota_fiscal.php?l_ext=117012360'>117012360<\/a>","Kelly","<a href='indicadorc.php?placa=TSET'>TSET<\/a>","8F","85",85,"293","2], ["004\/2012","<a href=nota_fiscal.php?l_ext=117012356'>117012356<\/a>","Kelly","<a href='indicadorc.php?placa=SLIVER_ADSL2'>SLIVER_ADSL2<\/a>","1C","222",222,"372","2], ...] hope you guys can help me, thanks! Edited January 3, 2013 by Diogom Quote Link to comment Share on other sites More sharing options...
requinix Posted January 3, 2013 Share Posted January 3, 2013 Step 1. If the dates aren't the same then sort by that. if ($a[0] != $b[0]) { list($am, $ay) = explode("/", $a[0]); list($bm, $by) = explode("/", $b[0]); return strcmp($by . $bm, $ay . $am); // reverse } Step 2. Sort by the ID number. ??? Don't know how you want to handle the alphanumeric ones. Step 3. Remove the array_reverse() stuff because you can do that in the sorting instead. Quote Link to comment Share on other sites More sharing options...
Diogom Posted January 3, 2013 Author Share Posted January 3, 2013 Step 1. If the dates aren't the same then sort by that. if ($a[0] != $b[0]) { list($am, $ay) = explode("/", $a[0]); list($bm, $by) = explode("/", $b[0]); return strcmp($by . $bm, $ay . $am); // reverse } Step 2. Sort by the ID number. ??? Don't know how you want to handle the alphanumeric ones. Step 3. Remove the array_reverse() stuff because you can do that in the sorting instead. thanks for the help, the result of this is the same, this will sort only the dates, the problem is how do sort the ID after that without screwing up the first sort.. i tried to regex because i thought i would do everything in the same function. alphanumeric ones arent really the problem, they can stay anywhere if the stay sorted by date correctly. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 3, 2013 Share Posted January 3, 2013 What is your sorting code now? Quote Link to comment Share on other sites More sharing options...
Diogom Posted January 4, 2013 Author Share Posted January 4, 2013 What is your sorting code now? still the same.. im thinking about grouping up them by date in several arrays, sort the arrays then glue them together, but its going to be a very ugly and messy code, wish i could think of something else. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 4, 2013 Share Posted January 4, 2013 still the same.. So you haven't changed anything... And you expect it to work differently? Quote Link to comment Share on other sites More sharing options...
cpd Posted January 5, 2013 Share Posted January 5, 2013 So you haven't changed anything... And you expect it to work differently? Because that's completely logical, durrr. You can sort the alphanumeric sting using the strnatcmp($str1, $str2) function. It's a natural order comparison. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.