正确地对名字和数字进行排序

发布于 2024-08-05 11:56:30 字数 404 浏览 4 评论 0原文

为了对项目名称进行排序,我想正确支持数字。即这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘东忘西忘不掉你 2024-08-12 11:56:30

这就是所谓的字母数字排序。
查看此链接:Alphanum 算法

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

自由如风 2024-08-12 11:56:30

我认为你可以使用一对对象,然后使向量>然后对这个向量进行排序。
配对根据其第一个元素进行比较。所以,这样你就可以获得你想要的类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文