对前导数字后跟非数字字符的字符串数组进行排序

发布于 2024-08-14 01:26:06 字数 368 浏览 1 评论 0原文

我可以使用以下代码获得排序的文件名列表:

$log_files = scandir(LLP_LOG_DIR);
$sorted = sort($log_files);

文件名格式为 X.log,其中 X 是渐进数值。

如何解决获得

0.log
1.log
10.log
11.log
2.log
3.log

所需结果

0.log
1.log
2.log
3.log
[..]
9.log
10.log
11.log
[..]

的问题我可以剥离“.log”字符串,对它们进行排序等,但最有效的方法是什么?

I can get a sorted filename list with the following code:

$log_files = scandir(LLP_LOG_DIR);
$sorted = sort($log_files);

the filename format is X.log, where X is a progressive numeric value.

How can I solve the problem of getting

0.log
1.log
10.log
11.log
2.log
3.log

where the wanted result is

0.log
1.log
2.log
3.log
[..]
9.log
10.log
11.log
[..]

I can strip the ".log" string, sort them etc, but what is the most efficient way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

自此以后,行同陌路 2024-08-21 01:26:06

尝试使用 nasort 代替,

natsort($log_files)

try natsort instead,

natsort($log_files)
倾城月光淡如水﹏ 2024-08-21 01:26:06

sort 的第二个参数设置为 SORT_NUMERIC数字排序:

$sorted = $log_files;
sort($sorted, SORT_NUMERIC);

请注意,sort 对第一个参数给定的变量数组进行排序。 sort 的返回值只是一个布尔值。

Set the second parameter of sort to SORT_NUMERIC to have numeric sorting:

$sorted = $log_files;
sort($sorted, SORT_NUMERIC);

And note that sort sorts the array of the variable given with the first parameter. The return value of sort is just a boolean value.

我纯我任性 2024-08-21 01:26:06

尝试使用 natcasesort 代替。

natcasesort($array);

因为 natsort 区分大小写。

Try natcasesort instead.

natcasesort($array);

because natsort is case sensitive.

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