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?

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.