English 中文(简体)
如何分类数字? [复制]
原标题:How to sort numbers? [duplicate]

如下:

<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>

<代码>sortNumber用于分类编号吗? 是什么意思,为什么存在? 为什么sortNumber in n.sort(sortNumber)n t 具体指明了ab的任何参数?

最佳回答

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.

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

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

并且不正确。

提供一种功能,sortNumber, 计票sort,每次<代码>sort<>/code>想要知道这两个项目中哪些是摆在另一个项目面前时,即以两个阵列要素标明该功能。

因此,sortNumber 提供了两个项目,即:<代码>数字操作,以换取

  • a negative value, meaning a is before b
  • a positive value, b is before a
  • zero: they re equal in terms of order (order doesn t matter).
问题回答

您需要考虑一下<代码>sort()的消耗量;sort(>的消费功能,其中界定了“der”。 简化:

array.sort(sortfunc)

因此,当你确定职能类别时,你实际上正在确定如何分类。

因此,如果我们确定一个机构被定义为:

return a - b;

我们正在要求通过 as令进行分类。

如果我们用身体确定职能:

return b - a;

我们正在要求以降临秩序为平地。

Hope this helps

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

Javascript s sort method takes an optional parameter: a function that compares two elements.
sort will 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.

这种职能接受2项论点,即<代码>a和b,并本应退还若干项。

  • if a is more than b then it should return any positive number.
  • if a is less than b then it should return any negative number.
  • if a is equals to b then it should return 0.

。 也可使用<条码>b-<条码>取而代之。 你们有这个想法。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签