2

I am trying to retrieve project numbers from a database in ascending order.

The rows look something like this:

-- project number --
       1199.7
       1053.2
       1020
       1023-D
       1023-C
       1023-B
       50
       34.3T
       870-W

Here is my query for retrieving and sorting the data:

$this->db->select('Project');
$this->db->from('`active_projects`');
$this->db->order_by('Project + 0, Project');

Output for part of the table:

enter image description here

My question is, why is 127 stuck in the middle and not at the top? I'm thinking that it is looking at it the same as 1270 just without the appending 0 but I'm not sure. Can anybody help me out in fixing it?

2 Answers2

2

Because your data type is string and ordered in alphabet order.

Use order_by('cast(Project as UNSIGNED), Project');

Nick
  • 9,735
  • 7
  • 59
  • 89
0

You can use the natsort() function from php.

$array = $result_from_query;

$sorted_array = natsort($array);

Now just return the sorted array.

nestedl00p
  • 490
  • 3
  • 14