Results 1 to 4 of 4

Thread: Sorting Clothing Sizes

  1. #1
    Join Date
    Feb 2014
    Thanked 0 Times in 0 Posts

    Default Sorting Clothing Sizes

    I have clothing sizes in a dB that I pull out and need to sort them in natural sizes.

    For example:

    Input Sizes: Youth - S, Adult-XL, Adult - M, Adult - L, Adult - S, Adult - 3XL, Adult - 2XL, Youth - M

    Sorted Sizes: Adult-S, Adult - M, Adult - L, Adult -XL, Adult - 2XL, Adult - 3XL, Youth - S, Youth - M

    Looking for a very simple way to do this. Any help appreciated!

  2. #2
    Join Date
    Jul 2010
    Thanked 21 Times in 21 Posts


    Should be able to use just a simple ORDER BY in the query. Unless the "Input sizes" is a string of data in a single row in the db. As long as each size is in it's own row in the db then use order by.
    SELECT * FROM `table` ORDER BY `field` ASC
    Obviously that is just an example.
    You never know everything, I learn everyday!

  3. #3
    Join Date
    Apr 2008
    Thanked 516 Times in 502 Posts
    Blog Entries


    Quote Originally Posted by jsnull View Post
    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:

    #  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. ... */ );
    $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`)?
    Last edited by traq; 05-09-2014 at 11:31 PM.

  4. #4
    Join Date
    Mar 2006
    Illinois, USA
    Thanked 690 Times in 678 Posts


    A better way to do this is to use numerical (or any sortable) values. So use sizes 1, 2, 3, etc. Even 1.5 or 1.75 if you want (to add in more as needed). Then just sort of smallest to largest (or the opposite).

    There there are two ways to convert these values to names:
    1. Include a second column in your table for the size names. Basically what you have now, plus the numbers column. Redundant and not so efficient, and slightly more work to manage, but very easy to change.
    2. Add a translation method from numbers to names. This is just like real language translation ("hello">"hola", etc.), but much smaller. So you could use an array in PHP to do this, or, if you're feeling like doing it the longer (and arguably more "correct") way, you could add a new database table of sizes<->numbers and work it out from there (even within MySQL then, so a query could show you directly what size something is).
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Similar Threads

  1. problem with font sizes and em
    By mayanktalwar1988 in forum CSS
    Replies: 1
    Last Post: 10-02-2009, 05:24 PM
  2. Tab Sizes for Tab Content Script
    By bconley in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 08-09-2007, 12:12 AM
  3. view 3 sizes of image - help please !
    By solder_on in forum JavaScript
    Replies: 4
    Last Post: 05-20-2007, 11:04 AM
  4. Image Sizes
    By ausdrew in forum Dynamic Drive scripts help
    Replies: 7
    Last Post: 04-14-2007, 04:47 AM
  5. Uniform button sizes?
    By Biblia in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-25-2005, 05:50 AM


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts