javascript - 按字母顺序对一行中的数字进行排序

标签 javascript php jquery

感谢您的耐心等待。

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

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

01-54-32-06-02-28-50-67 - ... ( +/- 32 numbers)
32-50-02-04-16-21-17-11 - ... ( +/- 38 numbers)
... ( +/- 800 lines)

正如你所看到的,每一行都有很多数字,用“-”分隔。并且该文件有很多行。这是一个多么荒谬的数额。

会将这些数字保留在有序的行中吗?示例:

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 来拯救你”,我也需要帮助。

非常感谢。

最佳答案

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

如果文件真的很大,这可能会消耗大量资源。

您可以使用 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
?>

关于javascript - 按字母顺序对一行中的数字进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22468229/

相关文章:

javascript - 使用 $.ajax 方法将变量从 javascript 传递到 php,以便在数据库 mySQL 中发布

javascript - 如何从jquery中的json中获取不同的值

jquery - Firefox 最大化不会触发调整大小事件

javascript - 使用 jQuery 在单击时切换 CSS 类不起作用

php - 检查是否仍安装了页面选项卡应用程序

javascript - $ ('#element' ).css ("CssProperty") 未加载

php - PDO 准备好的语句 - 参数名称中的冒号是做什么用的?

php - 将 MySQL 登录脚本转换为 PDO

javascript - apollo graphql 传递参数来解析函数

javascript - 无需遍历列表两次即可找到重复项?