小编Joh*_*all的帖子

如何对混合文本和数字(例如主机名)进行排序?

给定带有混合文本和数字(不带前导零)的输入,我如何才能按“自然”顺序对其进行排序?例如,给定以下输入(主机名):

whatever-1.example.org
whatever-10.example.org
whatever-11.example.org
whatever-12.example.org
whatever-13.example.org
whatever-2.example.org
whatever-3.example.org
whatever-4.example.org
whatever-5.example.org
whatever-6.example.org
whatever-7.example.org
whatever-8.example.org
whatever-9.example.org
Run Code Online (Sandbox Code Playgroud)

我想要这个输出:

whatever-1.example.org
whatever-2.example.org
whatever-3.example.org
whatever-4.example.org
whatever-5.example.org
whatever-6.example.org
whatever-7.example.org
whatever-8.example.org
whatever-9.example.org
whatever-10.example.org
whatever-11.example.org
whatever-12.example.org
whatever-13.example.org
Run Code Online (Sandbox Code Playgroud)

编辑

我应该提到,除了“随便”之外,还有

thingaroo-#.example.org
      .
      :

blargh-#.example.org
      .
      :

...etc...
Run Code Online (Sandbox Code Playgroud)

谢谢!

sort

3
推荐指数
1
解决办法
1752
查看次数

标签 统计

sort ×1