Re: sorting troubles

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

 



On Sat, April 22, 2006 4:49 am, William Stokes wrote:
> I have a column in DB that contains this kind of data,
> A20,B16,B17C14,C15,D13,D12 etc.
>
> I would like to print this data to a page and sort it ascending by the
> letter an descending by the number. Can this be done? Like
>
> A20
> B17
> B16
> C15
> C14
> D13
> D12

I personally would do it in SQL, since SQL has been specifically
fine-tuned for about 4 decades to do this [bleep] fast, while PHP has
not:

select col from DB order by substring(col, 1, 1), substring(col, 2)

You could also consider creating a "view" in your DB, if your DB
supports views.

If not, and the dataset is VERY large, and/or your particular database
does not allow a function in ORDER BY clauses, I would suggest you add
two indexed fields and break that one column up into two.

You'd need to add some kind of logic somewhere (triggers in the DB,
application logic, whatever) to manage the copying of the data from
the column to the other.

No matter how you slice it, though, the LAST option I'd use would be
to use http://php.net/usort to sort the data from a DB in PHP.  Still,
it's an option, if your DB is particularly broken, to the point where
none of the above apply. I don't think any such DB exists with PHP
support, unless maybe via ODBC, but I may as well provide the complete
answer, eh?

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
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