0

I have a Mysql table, I want to sort the values in the table by size of the meta_value from biggest to smallest.

Look at the order of the value? How come 150,000,000 is smaller than 1,555,555 .

enter image description here

DavSev
  • 1,005
  • 4
  • 22
  • 47
  • 2
    The left-alignment indicates that `meta_value` is a char field, not a number, so you're getting alphabetical sorting, which doesn't care if the value happens to represent a number. So, at the 3rd character, `0` gets sorted before `5`, and the rest is history. So, likely duplicate of [MySQL 'Order By' - sorting alphanumeric correctly](https://stackoverflow.com/questions/8557172/mysql-order-by-sorting-alphanumeric-correctly) – underscore_d Nov 12 '17 at 09:52

1 Answers1

1

Meta_value is a CHAR column, not a number column. If your meta value comes from another data source then you will need to use natural sorting, and you can use ORDER BY LENGTH(alphanumeric), alphanumeric

If you have control over the content in meta_value you could consider modifying the column data type to int or numeric or some other numeric data type. Then you would get the sort you expect. However, changing a data type with existing data runs the risk of encountering non-standard data, so you need to know exactly what values are in that column.

Oron Zimmer
  • 336
  • 1
  • 3