note 110801 added to function.usort
| From: | theternaryoperator | Date: | Mon, 10 Dec 2012 15:27:49 +0000 |
| Subject: | note 110801 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192441@lists.php.net to get a copy of this message | ||
This is a simple but effective algo to sort an array with strings which include date data. The
newest date is the first in the array.
<?php
usort($randomarray, function($a,$b)
{
//e.g. $randomarry[0] == (.)(.)MMDDYY(*.)
// -> note that a string can be seen as an array
return (($a[6] > $b[6]) ? -1 : (($a[6] < $b[6]) ? 1 : (($a[6] == $b[6]) ? (($a[7] > $b[7])
? -1 : (($a[7] < $b[7]) ? 1 : (($a[7] == $b[7]) ? (($a[2] > $b[2]) ? -1 : (($a[2] < $b[2])
? 1 : (($a[2] == $b[2]) ? (($a[3] > $b[3]) ? -1 : (($a[3] < $b[3]) ? 1 : (($a[3] == $b[3]) ?
(($a[4] > $b[4]) ? -1 : (($a[4] < $b[4]) ? 1 : (($a[4] == $b[4]) ? (($a[5] > $b[5]) ? -1 :
(($a[5] < $b[5]) ? 1 : (($a[5] == $b[5]) ? 0 : 0))) :0))) :0))) :0))):0))):0)));
});
?>
just have to modify the places of the datecomponents in the string.
----
Server IP: 87.106.1.137
Probable Submitter: 194.113.59.80
----
Manual Page -- http://php.net/manual/en/function.usort.php
Edit -- https://master.php.net/note/edit/110801
Del: integrated -- https://master.php.net/note/delete/110801/integrated
Del: useless -- https://master.php.net/note/delete/110801/useless
Del: bad code -- https://master.php.net/note/delete/110801/bad+code
Del: spam -- https://master.php.net/note/delete/110801/spam
Del: non-english -- https://master.php.net/note/delete/110801/non-english
Del: in docs -- https://master.php.net/note/delete/110801/in+docs
Del: other reasons-- https://master.php.net/note/delete/110801
Reject -- https://master.php.net/note/reject/110801
Search -- https://master.php.net/manage/user-notes.php