title: MySQL Order By With Grades As String But Want Integer
body: <p>I want to order grades that are a string in the following order.</p>
  
  <pre><code>K, 1, 2, 4, .... 12
  </code></pre>
  
  <p>But I keep getting <code>5, 4, 3, ... 12, K, 1</code></p>
  
  <p>The grade column in sims_classroom is VARCHAR(255) and the table is latin1;</p>
  
  <p>I have tried every cast and trick in the book. At the moment I have the following trick by adding 0 to it.  What am I doing wrong?</p>
  
  <pre><code>SELECT 
          (SELECT district_get_name_function(sod.parent_id, sod.id)) AS 'district_name', 
          (SELECT school_get_name_function(so.id)) AS 'school_name',
          st.teacher_username,
          st.teacher_first_name,
          st.teacher_last_name,
          c.name as 'classroom_name',
          c.grade,
          c.id,
          d.name AS 'discipline_name',
          d.id AS 'discipline_id',
          lc.name AS 'program_name',
          lc.id AS 'program_id'
      FROM sims_classroom c
  .
  .
  .
  
      ORDER BY 
              CASE lower(sort_direction) WHEN 'asc' THEN
                  CASE lower(sort_order)
                      WHEN 'grade' THEN 
                                   CASE c.grade 
                                      WHEN 'K' THEN 0
                                      ELSE (c.grade + 0)
                                   END
                      WHEN 'teachername' THEN lower(st.teacher_first_name)
                      ELSE c.name
                  END
              END ASC,
              CASE lower(sort_direction) WHEN 'desc' THEN
                  CASE lower(sort_order)
                      WHEN 'grade' THEN 
                                   CASE c.grade 
                                      WHEN 'K' THEN 10000
                                      ELSE (c.grade + 0)
                                   END
                      WHEN 'teachername' THEN lower(st.teacher_first_name)
                      ELSE c.name
                  END
              END DESC
  </code></pre>