Sorting names with numbers correctly

萝らか妹 提交于 2019-12-05 02:41:03

问题


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)


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/1453725/sorting-names-with-numbers-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!