Re: [PHP] sorting array question

2006-07-26 Thread Angelo Zanetti

hi guys,

thanks for the replies! yes Melanie, you were correct the last example 
(i had an old version of the manual). seemed to do the trick =)


Thanks to john as well for the reply.

Angelo

Melanie Maddix wrote:


[snip]
So they way I want to sort these rows is by total, totalPaid, 
totalUnpaid all descending.

Output should be:
partnerID  | partnerName | total  | totalPaid  | totalUnpaid
5  berty   301218
7  sarah   19127
1  marc   12  57




I have looked at the manual and found uasort and array_multisort, but 
these don't seem to be appropriate unless Im missing something but the 
examples found are very vague.

If anyone can help that would be great,

Thanks in advance
Angelo
[/snip]


Hi Angelo,

Take a look at Example 3 under array_multisort, the one that sorts by
volume and edition. 


You first build arrays of the columns you want to sort by, then pass
them as an argument to array_multisort, along with your original array.

Melanie

 



--

Angelo Zanetti
Systems developer


*Telephone:* +27 (021) 469 1052
*Mobile:*   +27 (0) 72 441 3355
*Fax:*+27 (0) 86 681 5885
*
Web:* http://www.zlogic.co.za
*E-Mail:* [EMAIL PROTECTED] 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting array question

2006-07-26 Thread John Wells

On 7/26/06, Angelo Zanetti <[EMAIL PROTECTED]> wrote:

Hi all,
So they way I want to sort these rows is by total, totalPaid,
totalUnpaid all descending.


Hi Angelo,

So the first question the list will ask is if the array of data is
coming from a database.  Because then you'd be urged to perform the
sorting from within your database query.

Barring that, you can use the usort() function to create custom
sorting.  What I've done below should do what you want, and I've tried
to generalize the functions a bit, so that you can mix descending and
ascending as you wish:

[code]
// Basic function for comparing arrays based on a particular column
function compareColumn($x, $y, $idx, $lessThan, $greaterThan)
{
if ( $x[$idx] == $y[$idx] )
 return 0;
else if ( $x[$idx] < $y[$idx] )
 return $lessThan;
else
 return $greaterThan;
}

// Calls compareColumn so that it sorts in descending order
function compareColumnDesc($x, $y, $idx)
{
return compareColumn($x, $y, $idx, 1, -1);
}

// Calls compareColumn so that it sorts in ascending order
function compareColumnAsc($x, $y, $idx)
{
return compareColumn($x, $y, $idx, -1, 1);
}

// Your customer comparison function.  The order in which you call each
// compareColumn*() is the ultimate order in which your multi-dim
array is sorted.
function compare($x, $y)
{
// total is index 2 of the array
$total = compareColumnDesc($x, $y, 2);
if($total != 0) return $total;

// totalPaid is index 3 of the array
$totalPaid = compareColumnDesc($x, $y, 3);
if($totalPaid != 0) return $totalPaid;

// totalUnpaid is index 4 of the array
$totalUnpaid = compareColumnDesc($x, $y, 4);
if($totalUnpaid != 0) return $totalUnpaid;

// arrays were equal
return 0;
}

$record = array(
array(1, 'marc', 12, 5, 7),
array(5, 'berty', 30, 12, 18),
array(7, 'sarah', 19, 12, 7)
);

// sort your array
usort($record, 'compare');

// print sorted array
echo '';
var_export($record);
echo '';
[/code]


HTH,
John W

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sorting array question

2006-07-26 Thread Angelo Zanetti

Hi all,

Need some advice as to how to sort a array:

Structure:

partnerID
partnerName
total
totalPaid
totalUnpaid


So basically there will be many entries for the following:

eg:

partnerID  | partnerName | total  | totalPaid  | totalUnpaid
1  marc   12  57
5  berty   301218
7  sarah   19127

So they way I want to sort these rows is by total, totalPaid, 
totalUnpaid all descending.

Output should be:
partnerID  | partnerName | total  | totalPaid  | totalUnpaid
5  berty   301218
7  sarah   19127
1  marc   12  57




I have looked at the manual and found uasort and array_multisort, but 
these don't seem to be appropriate unless Im missing something but the 
examples found are very vague.

If anyone can help that would be great,

Thanks in advance
Angelo


Angelo Zanetti
Systems developer


*Telephone:* +27 (021) 469 1052
*Mobile:*   +27 (0) 72 441 3355
*Fax:*+27 (0) 86 681 5885
*
Web:* http://www.zlogic.co.za
*E-Mail:* [EMAIL PROTECTED] 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting array question

2002-09-04 Thread timo stamm

Hi Javier,


I think parsing the listing into a multidemensional array is the 
answer. But actually, I do not yet know how that would look like 
in PHP. But I can give you an example of how it is done in 
ActionScript (Ecmascript).

file_arr[n] = [name, date]

Now you can sort file_arr by file_arr[n][0] (name) or 
file_arr[n][1] (date).


Hope it helps,
Timo


Am Dienstag den, 3. September 2002, um 12:27, schrieb Javier Montserat:

> i have the following code which reads a list of files from a 
> directory -
>
> $listcmd = "ls " .$dirPath;
> $temp = exec($listcmd, $listoffiles, $status);
> if($status == 0) {
> for ($i =0; $i < sizeof($listoffiles); $i++) {
>   $this->fileName[$i] = $listoffiles[$i];
>   $this->sizeofFile[$i] = sprintf("%01.2f", 
> (filesize($dirPath."/".$listoffiles[$i])/1024)/1024);
>   $this->fileDate[$i] = date("d-M-y H:i", 
> filemtime($dirPath."/".$listoffiles[$i]));
> }
> $this->displayFiles();
>
> What I want to do is display the files sorted by date.
>
> Okay, so I've just realised that the really easy way to do this 
> is by adding -St (sort by time) to the ls command...
>
> $listcmd = "ls -St " .$dirPath;
>
> But how could this be achieved by sorting the three arrays?  
> What if I alternately wanted to sort the files by name, size or 
> date without re-reading the directory each time?
>
> Would an associative array structure be better suited to this 
> type of operation?
>
> Thanks for your insight,
>
> Javier
>
> _
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
>
>
> -- PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] sorting array question

2002-09-03 Thread Javier Montserat

i have the following code which reads a list of files from a directory -

$listcmd = "ls " .$dirPath;
$temp = exec($listcmd, $listoffiles, $status);
if($status == 0) {
for ($i =0; $i < sizeof($listoffiles); $i++) {
   $this->fileName[$i] = $listoffiles[$i];
   $this->sizeofFile[$i] = sprintf("%01.2f", 
(filesize($dirPath."/".$listoffiles[$i])/1024)/1024);
   $this->fileDate[$i] = date("d-M-y H:i", 
filemtime($dirPath."/".$listoffiles[$i]));
}
$this->displayFiles();

What I want to do is display the files sorted by date.

Okay, so I've just realised that the really easy way to do this is by adding 
-St (sort by time) to the ls command...

$listcmd = "ls -St " .$dirPath;

But how could this be achieved by sorting the three arrays?  What if I 
alternately wanted to sort the files by name, size or date without 
re-reading the directory each time?

Would an associative array structure be better suited to this type of 
operation?

Thanks for your insight,

Javier

_
Chat with friends online, try MSN Messenger: http://messenger.msn.com


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php