Jump to content

[SOLVED] Sorting by price, £100 is less than £12???


fri3ndly

Recommended Posts

I have a website where users can sort data.

 

The price fields are VARCHARS and are displayed as follows:

 

£145.99

£129.99

£62.50

£84.00

£99.99

 

I can see the logic as to why MYSQL is sorting them with the 'least' at the top, but how do I owercome this?

I do not know any modification of the ORDER BY statement that would sort integers within strings. I can thik of the following solutions:

 

1) If possible, try either to split up the data colums into a currency and a value columns, then using a single character for $ or £ and a real number for the value. If all values have the same currency, omit it.

2) You could also extract the values to an array that is then sorted by sort($array).

3) If nothing helps, try inserting leading zeros after the currency sign, so that all strings have the same length. This is ugly, though. For a nice output, you would have to delete the zeros after sorting for output. Generally, I wouldn't recommend this.

 

Hope that helps!

 

I echo fenway's concern. Prices should be stored as numeric/double/float/int (probably not the latter).

"What about currency?" i hear you say, well then you need a currency field indicating the currency that the item price is in, i.e. GBP to £ or USD for $, or whatever the shortcode is for the currency.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.