Hi Monks,

I have a database table named location The table will look like

ID (Primary Key,
Auto Increment)
StateCity
1VirginiaNorfolk
2VirginiaChesapeake
3VirginiaVirginia Beach
4IndianaEvansville
5IndianaFort Wayne
6VirginiaFalls Church
7IndianaIndianapolis
8WashingtonSeatle
9WashingtonSpokane

I need to sort this table by primary key, but grouped by state. ie, the result will be

ID (Primary Key,
Auto Increment)
StateCity
1VirginiaNorfolk
2VirginiaChesapeake
3VirginiaVirginia Beach
6VirginiaFalls Church
4IndianaEvansville
5IndianaFort Wayne
7IndianaIndianapolis
8WashingtonSeatle
9WashingtonSpokane

Few things need to remember is

  1. We cant use SQL's "GROUP BY" as group by will return only one entry for "virginia", One for "Indiana" etc...
  2. We cant give SQL's "ORDER BY" as it wont group.
  3. We can not sort with State as it will take "Indiana" above "Virginia"
  4. I am NOT allowed to alter the structure of table.
  5. I am NOT allowed to use temperory tables or files
    1. Can anybody give some idea? I wish to have a "perl" solution.

      Thanks in advance :)

      --VC

      Updates

      • Edited the table to fix typo mistakes.
      • Added two entries in table(8 and 9) for clarificaton


      In reply to Sorting and Grouping with two different fields by atemon

      Title:
      Use:  <p> text here (a paragraph) </p>
      and:  <code> code here </code>
      to format your post, it's "PerlMonks-approved HTML":



    2. Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
    3. Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
    4. Read Where should I post X? if you're not absolutely sure you're posting in the right place.
    5. Please read these before you post! —
    6. Posts may use any of the Perl Monks Approved HTML tags:
      a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
    7. You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
              For:     Use:
      & &amp;
      < &lt;
      > &gt;
      [ &#91;
      ] &#93;
    8. Link using PerlMonks shortcuts! What shortcuts can I use for linking?
    9. See Writeup Formatting Tips and other pages linked from there for more info.