正确地对名字和数字进行排序
为了对项目名称进行排序,我想正确支持数字。即这个:
1 Hamlet
2 Ophelia
...
10 Laertes
而不是
1 Hamlet
10 Laertes
2 Ophelia
...
有人知道已经支持它的比较函子吗?
(即可以传递给 std::sort 的谓词)
我基本上有两种支持模式:前导数字(如上所述)和末尾数字,类似于资源管理器:(
Dolly
Dolly (2)
Dolly (3)
我想我可以解决这个问题:按字符进行比较,并以不同的方式处理数值,但是,这可能会破坏 unicode collaiton 等)。
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是所谓的字母数字排序。
查看此链接:Alphanum 算法
That's called alphanumeric sorting.
Check out this link: The Alphanum Algorithm
我认为你可以使用一对对象,然后使向量>然后对这个向量进行排序。
配对根据其第一个元素进行比较。所以,这样你就可以获得你想要的类型。
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.