javascript 如何在javascript排序方法中对数字进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4576903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:10:53  来源:igfitidea点击:

How to sort number in javascript sort method

javascriptfunctionsorting

提问by dramasea

Below is the code:

下面是代码:

<script type="text/javascript">

function sortNumber(a,b)
{
return a - b;
}

var n = ["10", "5", "40", "25", "100", "1"];
document.write(n.sort(sortNumber));

</script>

I found this is w3school with no explain ay all!! Is sortnumber function is formula of sorting a number? What's mean a and b and why it exist?why sortNumber in n.sort(sortNumber) doesn't specify any parameter of a and b?Can anyone explain it to me? Thanks!!

我发现这是 w3school,没有任何解释!!sortnumber 函数是对数字进行排序的公式吗?a 和 b 是什么意思,为什么存在?为什么 n.sort(sortNumber) 中的 sortNumber 没有指定 a 和 b 的任何参数?谁能给我解释一下?谢谢!!

采纳答案by e2-e4

The JavaScript sort()function may or may not take a parameter.
The parameter would be a function - meaning what function is to be used to assess which of two elements should be before the other.

JavaScriptsort()函数可能会也可能不会接受参数。
该参数将是一个函数含义what function is to be used to assess which of two elements should be before the other

The n array is made of strings representing numbers.
Doing a simple sort()without a function, an alphabetical order would be used: the result would be

n 数组由表示数字的字符串组成。
做一个sort()没有函数的简单,将使用字母顺序:结果将是

 "1", "10", "25"... "5"

and is not correct.

并且不正确。

Providing a function, sortNumber, tells sortto call that function with two elements of the array each time the sortalgorithm wants to know which of the two items is before the other.

提供一个函数,sortNumber告诉sort每次sort算法想知道这两个项目中的哪一个在另一个之前时,用数组的两个元素调用该函数。

Thus sortNumberprovided with two items, does a numericaloperation to return either

因此sortNumber提供了两个项目,执行numerical返回任一操作的操作

  • a negative value, meaning ais before b
  • a positive value, bis before a
  • zero: they're equal in terms of order (order doesn't matter).
  • 负值,意味着ab之前
  • a 正值,ba之前
  • 零:它们在顺序方面是相等的(顺序无关紧要)。

回答by Adrian Carolli

You need to consider what sort()consumes; sort()consumes a function that defines the sort oder. To simplify:

你需要考虑sort()消耗什么;sort()使用定义排序顺序的函数。为了简化:

array.sort(sortfunc)

So when you define the function sortNumber, you are actually defining how sort will sort the array.

因此,当您定义函数 sortNumber 时,您实际上是在定义 sort 将如何对数组进行排序。

So if we define a function that's body is defined as:

因此,如果我们定义一个函数,其主体定义为:

return a - b;

We are asking to sort by ascending order

我们要求按升序排序

If we define a function with body:

如果我们用 body 定义一个函数:

return b - a;

We are asking to sort by descending order

我们要求按降序排序

Hope this helps

希望这可以帮助

回答by SLaks

When you write n.sort(sortNumber), you're passing the sortNumberfunctionto sort.
You aren't calling the sortNumberfunction, so you don't pass any parameters.

当您编写 时n.sort(sortNumber),您将sortNumber函数传递给sort
您没有调用该sortNumber函数,因此您没有传递任何参数。

Javascript's sortmethod takes an optional parameter: a function that compares two elements.
sortwill call the function you pass to compare pairs of elements in the array.
The compare function that you supply should take two parameters and return a number indicating which one is bigger.

Javascript 的sort方法接受一个可选参数:一个比较两个元素的函数。
sort将调用您传递的函数来比较数组中的元素对。
您提供的比较函数应该接受两个参数并返回一个数字,指示哪个更大。

回答by Thai

The sort function accepts 2 arguments, aand b, and is supposed to return a number.

sort 函数接受 2 个参数ab, 并且应该返回一个数字。

  • if ais more than bthen it should return any positive number.
  • if ais less than bthen it should return any negative number.
  • if ais equals to bthen it should return 0.
  • 如果a大于b那么它应该返回任何正数。
  • 如果a小于b那么它应该返回任何负数。
  • 如果a等于b那么它应该返回0。

And a- bproduces that number. You can also sort it backwords by using b- ainstead. You get the idea.

并且a-b产生那个数字。您也可以使用b-a来对它进行排序。你明白了。