NumericalSort
NumericalSort[list]
把 list 的元素按数字顺序排序.
更多信息
- NumericalSort[list] 等价于 Sort[list,NumericalOrder].
- NumericalSort 用的是与 Sort 一样的经典顺序,除了它是按数值来处理数值表达式、日期和 Quantity 对象之外(与它们的表示无关).
- NumericalSort 适用于具有任意标头的表达式,不只是 List.
范例
打开所有单元 关闭所有单元基本范例 (2)
NumericalSort[{1, Pi, E, Infinity, -Sqrt[2], -Infinity}]Sort[{1, Pi, E, Infinity, -Sqrt[2], -Infinity}]NumericalSort[{Now, TimeObject[], Infinity, Sqrt[2], -1, 0, -Infinity, Quantity[1, "Meters"], Quantity[3, "Feet"], TimeObject[{1, 23, 45}], DateObject[{1618, 3, 3, 9, 8, 8}]}]范围 (7)
NumericalSort[{2, Infinity, 0., -GoldenRatio, -Infinity, 1 / 3}]NumericalSort[data[0, 1, Infinity, -Infinity]]NumericalSort[<|a -> 0, b -> 1, c -> Infinity, d -> -Infinity|>]NumericalSort[{h[0, 2], h[Infinity, 1], h[-Infinity, 3]}]NumericalSort[{h[0, 2, foo], h[Infinity, 1], h[-Infinity, 3]}]对于复值表达式,先按其实部进行排序,然后按虚部的绝对值进行排序:
data = RandomSample@Flatten@Outer[Complex, Range[-1, 1], Range[-2, 2]]n = Length[data]ComplexListPlot[Thread[Callout[data, Range[n]]], ...]sdata = NumericalSort[data]ComplexListPlot[Thread[Callout[sdata, Range[n]]], ...]NumericalSort[{Quantity[50, "Centimeters"], Quantity[0, "Meters"], Quantity[1, "Yards"], Quantity[2, "Feet"]}]NumericalSort[{Quantity[1, "Yards"], Quantity[1, "Minutes"], Quantity[50, "Centimeters"], Quantity[30, "Seconds"]}]NumericalSort[{Tomorrow, Today, Yesterday}]属性和关系 (2)
NumericalSort 用 NumericalOrder 作为排序函数:
NumericalSort[{b, a, Infinity, 0}]Sort[{b, a, Infinity, 0}, NumericalOrder]当用 Less 作为排序函数时也可以对数值表达式进行排序,但对于其他表达式则不一定:
Sort[{b, a, Infinity, 0}, Less]TakeSmallest 的结果是按 NumericalSort 排序的:
list = Sqrt /@ RandomInteger[100, 8]TakeSmallest[list, 8]% === NumericalSort[list]结果与 Sort 的结果可能不同:
Sort[list]可能存在的问题 (1)
用 NumericalSort 排序不能保证同一数字的不同表示的特定顺序:
list = {N[3, $MachinePrecision], 3, 3., N[3, 47]}NumericalSort[list]Sort[list]文本
Wolfram Research (2017),NumericalSort,Wolfram 语言函数,https://reference.wolfram.com/language/ref/NumericalSort.html.
CMS
Wolfram 语言. 2017. "NumericalSort." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/NumericalSort.html.
APA
Wolfram 语言. (2017). NumericalSort. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/NumericalSort.html 年
BibTeX
@misc{reference.wolfram_2026_numericalsort, author="Wolfram Research", title="{NumericalSort}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/NumericalSort.html}", note=[Accessed: 12-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_numericalsort, organization={Wolfram Research}, title={NumericalSort}, year={2017}, url={https://reference.wolfram.com/language/ref/NumericalSort.html}, note=[Accessed: 12-November-2026]}