Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting names with numbers correctly

For sorting item names, I want to support numbers correctly. i.e. this:

1 Hamlet
2 Ophelia
...
10 Laertes

instead of

1 Hamlet
10 Laertes
2 Ophelia
...

Does anyone know of a comparison functor that already supports that?
(i.e. a predicate that can be passed to std::sort)

I basically have two patterns to support: Leading number (as above), and number at end, similar to explorer:

Dolly
Dolly (2)
Dolly (3)

(I guess I could work that out: compare by character, and treat numeric values differently. However, that would probably break unicode collaiton and whatnot)

like image 562
peterchen Avatar asked Feb 22 '26 08:02

peterchen


1 Answers

That's called alphanumeric sorting.
Check out this link: The Alphanum Algorithm

like image 108
Nick Dandoulakis Avatar answered Feb 23 '26 23:02

Nick Dandoulakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!