0

i want to sort a list that has numeric and alphanumeric items like this:

  • PIEZA1
  • 0001
  • PIEZA20
  • REF30
  • 234
  • REF2
  • FDGSDFG
  • PIEZA3
  • REF0004
  • REF2005

And this is how i want to stay:

  • 0001
  • 234
  • FDGSDFG
  • PIEZA1
  • PIEZA3
  • PIEZA20
  • REF0004
  • REF2
  • REF30
  • REF2005

But, when i use the 'order by' clause, the sort appears like this:

  • 0001
  • 234
  • FDGSDFG
  • PIEZA1
  • PIEZA20
  • PIEZA3
  • REF0004
  • REF2
  • REF2005
  • REF3

That order is because the sentence detects a string list, and the natural sort for strings mades it with alphabetical order

I don't know how to separate numeric with strings for sort, first with numeric and second with alphanumeric order

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
D. Vazquez
  • 9
  • 1
  • 2

2 Answers2

0

Is a a natural Sort what yiou want ? Try using Order by Length(column), column.

In Fact this is just the Short Version that does not always works as expected. If you want the REAL natural sort, use this: Natural Sort in MySQL

Community
  • 1
  • 1
Alexis Peters
  • 1,583
  • 1
  • 10
  • 17
0

You can try to cast your alphanumeric column to bytea datatype and try like this:

SELECT colname
FROM tablename
ORDER BY colname::bytea;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331