简体   繁体   English

有人可以解释 javascript 排序方法是如何工作的吗?

[英]can someone explain how javascript sort method works?

I have encountered this javascript code involving a sort method with a custom return.我遇到了这个 javascript 代码,其中涉及带有自定义返回的sort方法。

const nums =  ['9', '5', '3', '34', '30' ];
const num = nums.map(n => n.toString()).sort((a,b) => a + b < b + a? 1: -1).join('')

Essentially, this code is returning the largest possible integer.本质上,此代码返回最大可能的 integer。 I have the basic knowledge of sort, if you want the numbers in ascending order then you'll use ab for the return.我有排序的基本知识,如果您希望数字按升序排列,那么您将使用ab进行返回。 If descending is desired, then you would use ba .如果需要降序,那么您将使用ba

I want to know how the a and b are working behind the scenes.我想知道ab是如何在幕后工作的。 I couldn't find a source where it explains in details how the sort works.我找不到详细解释排序如何工作的来源。 I would like to know step by step when the sort is in action;我想逐步了解sort何时起作用; it will give me a better a idea how the code above works.它会让我更好地了解上面的代码是如何工作的。

Your help will be appreciated.您的帮助将不胜感激。

Details can be found in the spec .详细信息可以在规范中找到。 Basically, sort works by calling the callback repeated to compare two entries from the array.基本上, sort通过调用重复的回调来比较数组中的两个条目来工作。 The callback is supposed to return -1 if a is "less than" b (according to whatever rules that particular callback wants to apply for what "less than" means), 0 if they're the same, or 1 if a is "greater than" b .如果a "小于" b (根据特定回调想要应用的任何规则来表示 "小于" 的含义),回调应该返回 -1,如果它们相同,则返回 0,如果a是 ",则返回 1"大于" b . It continues doing that, with different pairs of entries from the array, until it's sorted the array.它继续这样做,使用数组中的不同条目对,直到它对数组进行排序。

How sort does that is up to the implementation, it's not dictated in the spec.如何sort取决于实现,规范中没有规定。 All the spec dictates is the paragraph above, and that (as of quite recently) the sort must be stable.所有规范都规定了上面的段落,并且(最近)排序必须是稳定的。

A couple of notes on the specific code you've shown:关于您显示的特定代码的一些注释:

  • arr should be nums in the first line arr应该是第一行中的nums
  • There's no point to the map call, the entries in the array are already strings map调用没有意义,数组中的条目已经是字符串
  • The comparisons it's doing are alphabetic它所做的比较是按字母顺序进行的
  • The sort method is incorrectly coded, because if a and b are the same, it returns -1, not 0 sort方法编码错误,因为如果ab相同,它返回 -1,而不是 0

You yould take a little different callback which adds the two values in different order and returns the delta of both.你会采取一些不同的回调,它以不同的顺序添加两个值并返回两者的增量。

It works by getting a part string of the final result and changes the order for getting the greatest value of both items.它通过获取最终结果的部分字符串来工作,并更改顺序以获得两个项目的最大值。

For example, the given data sorts in EDGE in this order:例如,给定的数据在 EDGE 中按以下顺序排序:

 abb + aa + b delta comment array -- -- ----- ----- ----- ------- -------------- 3 5 9 30 34 3 5 53 35 -18 switch 5 3 9 30 34 3 9 93 39 -54 switch 5 9 3 30 34 5 9 95 59 -36 switch 9 5 3 30 34 3 30 303 330 27 keep 9 5 3 30 34 30 34 3430 3034 -396 switch 9 5 3 34 30 5 34 345 534 189 keep 9 5 3 34 30 3 34 343 334 -9 switch 9 5 34 3 30

 const array = ['3', '5', '9', '30', '34'], result = array.sort((a, b) => (b + a) - (a + b)).join(''); console.log(...array); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以解释这个 JavaScript 代码是如何工作的吗? - Can someone explain how this JavaScript code works? 有人可以解释这个javascript闭包是如何工作的吗? - Can someone explain how this javascript closure works? 有人能解释一下“forEach”方法在下面的代码中是如何工作的吗? - Can someone explain how the “forEach” method works in the code below? 有人可以向我解释逻辑“OR”运算符的范围在 Javascript 中是如何工作的吗? - Can someone explain to me how the scope of the logical "OR" operator works in Javascript? 有人可以解释一下此JavaScript / Angular代码如何工作吗? - Can someone explain how this JavaScript/Angular code works? 有人可以解释一下 reduce 方法在这个 Functional JavaScript 挑战中是如何工作的吗? - Can someone explain how the reduce method is working in this Functional JavaScript challenge? 有人可以解释这段代码是如何工作的吗? - Can someone explain how this code even works? 有人可以解释setInterval计数器的工作原理吗? - Can someone explain how the setInterval counter works? 有人可以解释一下这个 stopPropagation 是如何工作的吗? - can someone explain how this stopPropagation works? 有人可以解释这个 for 循环是如何工作的吗? - Can someone explain how this for loop works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM