Re: Sorting...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> array (
>          20040310, Title, Author
>          20041115, Title, Author
>          20040513, Title, Author
>       )
> 
> where each array element is 1 line from the csv. When I go to print it
> out, Im going to explode each by , and then print it out the way I like.
> 
> BUT, I want to sort them all by those first 8 digits, which happen to be
> the date. Any way to sort an array by the first x characters in its
> string?
> 
> This would save a lot on processor time, otherwise Im going to have to
> pull an array of those characters, sort it, and then run some matching
> scheme against the CSV data to print it in the order I like - which would
> be HUGELY processor intensive.

I think sort should do it for you

$array = array("20040310, Title, Author",
                "20041115, Title, Author",
        	"20040513, Title, Author");
sort($array);
reset($array);
while (list($key, $val) = each($array)) {
   echo "array[" . $key . "] = " . $val . "<br />";
}

but, if you wanted to just make sure it was the first entry in the CSV try usort

function cmp($a, $b)
{	

   $first = explode(',', $a);	
   $second = explode(',', $b);	
	
   if ($first == $second) {
       return 0;
   }
   return ($first < $second) ? -1 : 1;
}


$array = array("20040310, Title, Author",
                "20041115, Title, Author",
        	"20040513, Title, Author");
usort($array, "cmp");

while (list($key, $value) = each($array)) {
   echo "array[" . $key . "] = " . $value . "<br />";
}

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux