C 中的自然排序 - “包含数字和字母的字符串数组”
我正在寻找一种经过验证的生产算法。 我确实看到了这个示例 但我在网络或书籍上找不到太多其他内容。
IE file_10.txt >文件_2.txt
I am looking for a proven algorithm for production.
I did see this example
but I'm not finding much else on the web or in books.
i.e.
file_10.txt > file_2.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个(经过测试的)比较函数,可以完成这项工作。它只理解无符号整数,而不理解有符号整数或浮点:
如果你想将它与
qsort
一起使用,请使用这个辅助函数:你可以按照以下顺序执行一些操作
Here is a (tested) comparison function that does the job. It understands only unsigned integers, not signed integers or floating point:
If you want to use it with
qsort
, use this auxiliary function:And you can do something on the order of
基本排序函数是标准 C
qsort()
。它被参数化以采用比较函数,而比较函数是您需要编写的来进行自然排序的函数。您的交叉引用问题包括比较函数的 C 实现。
Google 搜索“natural sort c”显示 SourceForge 实现。
The basic sort function would be standard C
qsort()
. It is parameterized to take a comparison function, and the comparison function is what you would need to write to do the natural ordering.Your cross-referenced question includes a C implementation of a comparison function.
A Google search 'natural sort c' shows a SourceForge implementation.
我假设您已经了解 C 标准库
qsort()
函数:最后一个参数是一个函数指针,这意味着您可以将任何函数传递给它。事实上,您可以使用
strcmp()
,但这会给您 ASCIIbetical,并且您特别需要自然排序。在这种情况下,您可以很容易地编写一个:
如果您早点
返回
,一些else
就可以被清除,但有一个基本结构。检查ctype.h
中的函数,例如isalpha()
(如果字符是字母表的一部分)、isdigit()
、isspace ()
等等。I assume you already know the C standard library
qsort()
function:That last parameter is a function pointer, which means you can pass any function to it. You could use
strcmp()
, in fact, but that would give you ASCIIbetical, and you specifically want a natural sort.In that case, you could write one pretty easily:
Some of the
else
s could be cleared up if you justreturn
early, but there's a basic structure. Checkctype.h
for functions likeisalpha()
(if a character is part of the alphabet),isdigit()
,isspace()
, and more.这是 Qt 的版本,也支持 unicode:
}
Here's a version for Qt, that also supports unicode:
}