Order a column name with mysql -


i getting website called codefights gives developers challenge. wanted ask lovely people @ stack overflow maybe steer me in right direction mysql challenge.. first gives, description , if link works you, maybe can me solve question because take long type.

if not answer 50% of question simply: select chapter_name book_chapters;

it requires order chapter name in ascending order , i've tried select chapter_name book_chapter order chapter_number asc; no luck, want 1 column book_chapters , its, chapter_name.

a little stumped.

one approach convert roman numerals arabic (base 10) representation.

but isn't necessary.

a series of simple string replacements trick.

in terms of ranking roman numeral "digits" ...

    < v < x < l < c < d < m 

if replace 'l' 'y' , 'c' 'z', , (for now) ignore 'd' , 'm', string comparison hold true:

    'i' < 'v' < 'x' < 'y' < 'z'   

the trick here we're selecting replacement characters in specific collating sequence, can sort.

the other issue have deal special cases of "subtraction" (backwards) replacements

    iv     ix     xl     xc 

if replace cases representations 4 repeated characters... i.e. iv -> iiii, ix -> viiii, etc. gets string values can sorted.

so, use order clause this:

  order replace(              replace(                replace(                  replace(                    replace(                      replace(                        upper( chapter_num )                       ,'c','z')                    ,'l','y')                  ,'iv','iiii')                ,'ix','viiii')              ,'xy','xxxx')            ,'xz','yxxxx') 

this works roman numeral values 399. that's when run 'd'.

to handle numbers 400 or greater, replace 'd' character "greater than" 'z' in collating sequence, such left square bracket '['. , can replace 'm' right square bracket ']'. , make appropriate replacements special cases 'cd' , 'cm'.

we need verify collating sequence character strings, holds true:

    'i' < 'v' < 'x' < 'y' < 'z' < '[' < ']' 

then can extend support roman numerals beyond 399 ...

  order replace(              replace(                replace(                  replace(                    replace(                      replace(                        replace(                          replace(                            replace(                              replace(                                upper( chapter_num )                              ,'m',']')                            ,'d','[')                          ,'c','z')                        ,'l','y')                      ,'iv','iiii')                    ,'ix','viiii')                  ,'xy','xxxx')                ,'xz','yxxxx')              ,'z[','zzzz')            ,'z]','[zzzz') 

Comments