
Originally Posted by
jsnull
I have clothing sizes in a dB that I pull out and need to sort them in natural sizes.
… Adult-S, Adult - M, Adult - L, Adult -XL, Adult - 2XL, Adult - 3XL, Youth - S, Youth - M
Honestly, that doesn't seem "natural" to me: wouldn't Youth S, M come before Adult S?
In any case, you're kinda "backed into a corner," here: there's no straightforward solution.
How large are your result sets? If they're not too big (and never will be), then you can sort them in your application code:
PHP Code:
<?php
# pseudocode! #
// when retrieving rows from your query, index them by size:
while( $list( $item,$size,$etc /* etc. ... */ ) = $result->fetch() ){
$clothing[$size] = array( "item"=>$item,"size"=>$size,"etc."=>$etc /* etc. ... */ );
}
// then put the indexes in the desired order:
$sort_order = array( "Adult-S","Adult-M","Adult-L" /* etc. ... */ );
foreach( $sort_order as $size ){
$sorted_clothing[$size] = $clothing[$size];
}
// clean up
unset( $clothing );
// everything's in order
This will kill you quick for large result sets, however. It would be much more efficient to let the database sort it. This will depend on how your DB is designed, however (and will probably require changes).
Can you show us your DB schema (use SHOW CREATE TABLE `table_name_goes_here`
)?
Bookmarks