note 93464 rejected from function.usort by thiago
| From: | thiago@php.net | Date: | Fri, 11 Sep 2009 13:24:19 +0000 |
| Subject: | note 93464 rejected from function.usort by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160457@lists.php.net to get a copy of this message | ||
Note Submitter: mailorder at bestpicks dot net
----
I want to sort a file that has a number with information in decending order by the number and
file.txt looks like this:
-255 | Phils bills
3437 | joes account
0 | mikes spending
-10 | Sams spending
212 | Jims account
The following php is how I do it and it partially works. The positive numbers list high to low,
followed by the zeros, but afterwards all the negative numbers are low to high. How can I fix that
part?
$filenmef = temp.txt';
$handle = fopen($filenmef, 'w');
$namesfile = file.txt";
$nfopen = file($namesfile);
foreach ($nfopen as $line_num => $line) {
list($rank, $webname) = split (" [|] ", $line);
}
natsort($nfopen);
for ($n=0; $n<count($nfopen); $n++) {
$place = each ($nfopen);
fwrite($handle,"$place[value]");
}
fclose($namesfilef);
$filenmef = file.txt';
$handle = fopen($filenmef, 'w');
$namesfile = temp.txt";
$nfopen = file($namesfile);
$nfopen = array_reverse($nfopen);
foreach ($nfopen as $rank) {
fwrite($handle,"$rank");
}
fclose($namesfilef);