11

如何对这种输入进行排序?

0.00159265291648695254
-0.00318530179313823899
0
0.00999983333416666468
0.00362937767285478371
0.00477794259012844049
-0.00637057126765263261
0.00681464007477014026
-0.00840724736714870645
-0.00522201549675090458

要么sort -n datasort -g data产生这个:

0
0.00159265291648695254
-0.00318530179313823899
0.00362937767285478371
0.00477794259012844049
-0.00522201549675090458
-0.00637057126765263261
0.00681464007477014026
-0.00840724736714870645
0.00999983333416666468

另一方面-1.whatever将在零之前。我需要那种注意到减号的东西。谢谢你。

4

3 回答 3

23

所有这些麻烦都做了我的本地设置。我的 ubuntu 是捷克语:

$ echo $LANG
cs_CZ.UTF-8

在这个本地设置中,它不是小数点,而是一个将整数与其他整数分开的小数逗号(正如我们在数学课中所认为的那样,在我们的语言中,我们确实写的是逗号而不是点)。

所以:

echo '0,03 >> 0,4 >
> -0,3 >
> 0' | sort -n
> 0
> -0,3 >
> 0,4 >
0,03 >

如果您正在编写 bash 脚本,请将排序例程设置为使用“正常”设置。

export LC_ALL=C
于 2012-04-05T15:48:41.797 回答
2

问题可能出在您的排序命令中。如果我运行相同,我的结果与预期的一样:

$ echo '0.00159265291648695254
> -0.00318530179313823899
> 0
> 0.00999983333416666468
> 0.00362937767285478371
> 0.00477794259012844049
> -0.00637057126765263261
> 0.00681464007477014026
> -0.00840724736714870645
> -0.00522201549675090458' | sort -n
-0.00840724736714870645
-0.00637057126765263261
-0.00522201549675090458
-0.00318530179313823899
0
0.00159265291648695254
0.00362937767285478371
0.00477794259012844049
0.00681464007477014026
0.00999983333416666468

如果不使用它,你应该使用 GNU 排序

sort (GNU coreutils) 5.93
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and Paul Eggert.
于 2012-04-05T15:15:17.233 回答
1

我有同样的问题。对于 1.0 和 -1.0 之间的数字。sort -n(或-g)不能解决它。我终于把所有的东西都乘了一个大数,排序,然后再除。尽管如此,我还是不明白为什么linux会出现这样的错误

于 2020-11-20T16:09:41.007 回答