Jump to content

Sorting an array


Diogom

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/272657-sorting-an-array/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/272657-sorting-an-array/#findComment-1403043
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/272657-sorting-an-array/#findComment-1403047
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.