1

I want to order by the title value. This is a string. But when I use the orderBy() method of the phalcon query builder the array is not ordered properly like '-1, -2, -3, -10, -11, -12'. Is there a other way to order strings using the builder?

$Query = Options::query()->orderBy('title')->execute();

The array that I wan't order by the title:

  array
    0 =>
        array
            'id' => string '1'
            'title' => string '-1' 
      1 =>
        array
            'id' => string '2'
            'title' => string '-10' 
      2 =>
        array
            'id' => string '3'
            'title' => string '-11' 
      3 =>
        array
            'id' => string '4'
            'title' => string '-2' 
      4 =>
        array
            'id' => string '5' 
            'title' => string '-3' 
      5 =>
        array
            'id' => string '6' 
            'title' => string '-12'
CodeWhisperer
  • 1,143
  • 2
  • 19
  • 39

1 Answers1

0

This is the age old issue of ordering a string numerically. You can either use some custom SQL to force the field to order numerically (like Volodymyr's comment), or you can change your database schema so that your title is numeric. Both of these solutions only work if the title will always be numeric, which I don't know if it will be the case from what you've given us.

Nathan Edwards
  • 311
  • 1
  • 3
  • 17