SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorting problem...

    I am having a sorting problem, adn am hoping one of you whom are out of the box could help me...

    I have a database of cartridges, and need to be able to sort them properly... because they're NOT behaving now

    Right now I'm using this SQL:
    PHP Code:
    $sql "SELECT Cartridge FROM $table SORT BY Cartridge"
    And It returns this using the basic while statement:

    Rifle Cartridges
    22 Hornet
    218 Bee
    222 Remington
    223 Remington
    22-250 Remington
    30-06 Springfield
    35 Remington
    7.62 x 39mm Russian
    280 Remington
    25-06 Remington
    7mm Remington Magnum


    Is there any way I can just ignore the decimal places, then sort like, by the first two charachters, then within each group of the same two charachters sort within them? And sort it to something like this?:

    218 Bee
    22 Hornet
    22-250 Remington
    222 Remington
    223 Remington
    25-06 Remington
    280 Remington
    30-06 Springfield
    35 Remington
    7.62 x 39mm Russian
    7mm Remington Magnum

    I am really hoping so... there's got to be a way to do it. Even if someone has a really good tutorial, don't worry about writing me a script... just point me to it... I'm not real good, but I'm hoping I can figure it out!

    Thanks again to all!

    Alex Stanton
    webmaster
    Last edited by exbabylon; Feb 25, 2001 at 22:22.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First off I'll assume you're using ORDER BY, because there's no such thing as SORT BY in MySQL's implementation of SQL.

    Basically you're going to have to using MySQL's string manipulation functions to mash your cartridge descriptions into a format that can be sorted the way you want to.

    As a basic example, if you wanted to remove decimal points (.) and dashes (-) from the field for sorting purposes, you'd end your query with something like this:

    ... ORDER BY REPLACE(REPLACE(Cartridge,'.',''),'-','')

    Of course, your expression will probably be more complex before it works exactly the way you want it to, but this should get you started. Post back if you get stuck!
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


Bookmarks

Posting Permissions

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