按行按字母顺序排序

时间:2014-03-18 00:35:05

标签: javascript php jquery

我要感谢你的耐心等待。

最初我需要 PHP或Javascript或jQuery 的答案。

我有一个超过800行的.txt文件,每行都有多个数字,例如:

  

01-54-32-06-02-28-50-67 - ...(+/- 32个数字)
  32-50-02-04-16-21-17-11 - ...(+/- 38个数字)
  ......(+/- 800行)

正如您所看到的,每行都有很多数字,用' - '分隔。而且文件有很多行。什么是荒谬的。

将这些数字留在有序行中吗?例如:

  

01-02-06-28-32-50-54-67 -...
  02-04-11-16-17-21-32-50 -...
  ...

不想在数据库中注册(对于PHP),或者手动完成所有操作。需要太长时间,也许我会老去解决这一切。的:)

是否有任何工具可以对.txt文件进行排序。我在想什么,例如:

  

/ 01-54-32-06-02-28-50-67 -...
  / 02-50-02-04-16-21-17-11 -...
  ...

'/'将成为开始组织生产线的触发器。我不知道我在胡说八道。

我设法找到了一个类似的工具。这个工具它命令所有数字,将所有数字保留在同一行。我不想要的。
  http://www.textfixer.com/tools/alphabetical-order.php

页面的代码来自这里:
  http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support/

我需要一个答案。即使它是'男人,也没有只有NASA来救你',我需要帮助。

非常感谢。

1 个答案:

答案 0 :(得分:2)

此脚本将从文件中筛选每一行并对其进行排序。

如果文件非常大,这可能会占用大量资源。

您可以使用MySQL数据库进行简单的逐行排序,而不是从文件中获取。

如果您需要任何帮助,请告诉我。

<?php
// Getting Data from a file 'data.txt' into $content variable
$content = file_get_contents('data.txt');

// $info - array for storing final info
$info = array();

// Putting all lines in $info - put / at the end of every line or change the delimeter from '/' to anything else
$info = explode('/', $content);

// Going through All lines one by one - Sorting it and putting it back on the array
foreach ($info as $key => $value) {
    $number = explode('-', $value);
    sort($number);
    $info[$key] = implode('-', $number);
}

// Echo - Save to File

print_r($info); //Echo'ing result

file_put_contents('final.txt', implode("\n/",$info)); //Putting it into file
?>
相关问题