tags:

views:

1691

answers:

7

e.g,

foo1
foo2
foo10
foo100

rather than

foo1
foo10
foo100
foo2

Update: not interested in coding the sort myself (although that's interesting in its own right), but having the database to do the sort for me.

+1  A: 

You can use functions in your order-by clause. In this case, you can split the non-numeric and numeric portions of the field and use them as two of the ordering criteria.

select * from t
 order by to_number(regexp_substr(a,'^[0-9]+')),
          to_number(regexp_substr(a,'$[0-9]+')),
          a;

You can also create a function-based index to support this:

create index t_ix1
    on t (to_number(regexp_substr(a, '^[0-9]+')),
          to_number(regexp_substr(a, '$[0-9]+')), 
          a);
Mark Harrison
If you want SQL answers, you should probably clarify your question to specify that. Or do you want to collect a variety of sorting techniques?
Justin Voss
He posted the question and answer at the same time. He is either looking to share this bit of knowledge he discovered, collect rep points, or both.
postfuturist
Not to be impolite, but it is tagged as SQL.
kchau
Be that as it may, a bit more of a description in the question wouldn't hurt
Davy8
Ah, but more question would detract from the answer...
Jonathan Leffler
Doesn't $[0-9]+ match digits after the end of the line and so always be null? Also, this doesn't work if there is more than one group of digits.
Joseph Bui
A: 

In PHP, the natsort function will sort an array of strings the way a human would, instead of in ASCII order.

Justin Voss
You're wasting your time, it was a rhetorical question.
Menkboy
PHP isn't built into Oracle SQL, I think. This limits the benefit form this solution.
Jonathan Leffler
+2  A: 

Jeff also has a post on the topic, with more resources for other languages.

Tom Ritter
A: 

A related question - which sort algorithm works best on mostly sorted data?

Edit

This is now a separate question: Which sort algorithm works best on mostly sorted data

graphics
A: 

I keep seeing "solutions" to this problem that involve trying to find the first numbers in the string. But sometimes you have strings like "123_456_7" vs. "123_456_10" vs. "123_8_10", and a "natural sort order" would do:

123_8_10 123_456_7 123_456_10

while alphabetical sort would do 123_8_10 123_456_7 123_456_10

What we really need is for Oracle to implement natural sort order into their product so that it is available inherently. This would also allow indexes to be built on the natural sort order. As it is, we all have to do tricks (0-pad; "to_number" and substring, etc.).

A: 

According to a colleague of mine you can achieve natural sorting in Oracle and MySQL like this:

ORDER BY fieldName + 0 ASC

I haven't tried it myself, but I guess it's worth a shot.

Mallox
That doesn't work.
John Topley