Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Sorting and Grouping with two different fields

by arkturuz (Curate)
on Jul 12, 2007 at 12:10 UTC ( #626217=note: print w/replies, xml ) Need Help??

in reply to Sorting and Grouping with two different fields

Notice that if id field is 'auto_increment' you cannot have two 7s. Also, it's better to use SQL on your data as they might increase in uncontrollable way. That's what the databases are for in the first place.
For your data you can use SQL like this (MySQL): ORDER BY state DESC, id ASC
Implementing this in Perl would be a waste of time, as the database can do it for you already.

Replies are listed 'Best First'.
Re^2: Sorting and Grouping with two different fields
by vcTheGuru (Chaplain) on Jul 12, 2007 at 12:48 UTC

    I can not use "OREDR BY state DESC" so that Virginia will come up. When I sort by state with " DESC state, id ASC " the order of states will be "Washington, Virginia,Indiana". But I need it sorted by id, but grouped with state.


      I would just add another field SMALLINT from 1-50 called sort_order and then you can apply your arbitrary sort order to each state...
      UPDATE table1 SET sort_order = 1 WHERE state = "Virginia"
      Then you can
      ORDER BY sort_order DESC, city DESC

        Your answer is too specific. If its dealing with only 50 states of US, Its OK to assign 1-50 called sort order. Here I am NOT allowed to alter the structure of the table as there are many other programs that depends on no of columns and other structure of the table. I am NOT allowed to add any temperory table. We wont mind the overhead caused by the sort algorith implemented. What I am looking for is a logic like Limbic~Region suggested.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://626217]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-06-24 22:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.