peterchen
peterchen

Reputation: 41116

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)

Upvotes: 3

Views: 740

Answers (2)

user855
user855

Reputation: 19938

i think u can use a pair object and then make vector > and then sort this vector. Pairs are compared based on their first elements. So, this way you can get the sort you desire.

Upvotes: 0

Nick Dandoulakis
Nick Dandoulakis

Reputation: 43130

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

Upvotes: 5

Related Questions