Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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.


Comment on Re: Sorting and Grouping with two different fields
Download Code
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.

    --VC

      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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://626217]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-07-26 12:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (176 votes), past polls