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!

Edited by Diogom
Link to comment
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
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
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.