Sujeet Kumar
Sujeet Kumar

Reputation: 1320

order_by() is not working properly for distance range data

I have data for distance in range like

5-10, 0-5 , 10-15,

I am using this query to get data in sorted order:

$this->db->select("distance_from_mall");
$this->db->from("transport_charges");
$this->db->order_by("distance_from_mall", "asc");
$route =$this->db->get()->result_array();

it gives me

0-5 , 10-15,5-10 instead of 0-5 ,5-10, 10-15,

Upvotes: 0

Views: 49

Answers (1)

Arth
Arth

Reputation: 13110

Because your distance is a string, the engine ORDERs it as a string.

That is the correct response for this query.

I'd suggest changing the distance to the minimum integer/numeric for each transport_charge.. this will give you all the data you need if the ranges don't overlap.

If they do; store the distance as two columns, range_max and range_min, and ORDER BY whichever makes more sense or both columns separately: ORDER BY range_min, range_max

Upvotes: 1

Related Questions