aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference
diff options
context:
space:
mode:
authorliangjiancang <42267675+liangjiancang@users.noreply.github.com>2021-10-03 03:12:17 +0800
committerGitHub <noreply@github.com>2021-10-03 03:12:17 +0800
commitf47edc78927b0833d6c48f16b9c22135505d6b02 (patch)
tree3a53d8ddb2e7c965525a4e0c5f9bb9bb2a769e7c /files/zh-cn/web/javascript/reference
parent64b71a0cee97a8d82fca220c12148042d9dc9981 (diff)
downloadtranslated-content-f47edc78927b0833d6c48f16b9c22135505d6b02.tar.gz
translated-content-f47edc78927b0833d6c48f16b9c22135505d6b02.tar.bz2
translated-content-f47edc78927b0833d6c48f16b9c22135505d6b02.zip
Update Web/JavaScript/Reference/Global_Objects/Array/sort, zh-CN (#2611)
* 补充缺失的「Sort stability」部分 * 与原文 HTML 保持一致 * Fix:「可以会」→「可能会」
Diffstat (limited to 'files/zh-cn/web/javascript/reference')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/array/sort/index.html67
1 files changed, 53 insertions, 14 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/sort/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/sort/index.html
index e9f9808420..e4140bde3b 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/sort/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/sort/index.html
@@ -114,20 +114,20 @@ items.sort(function (a, b) {
return (a.value - b.value)
});
-<code class="language-js"><span class="comment token">// sort by name</span>
-items<span class="punctuation token">.</span><span class="function token">sort</span><span class="punctuation token">(</span><span class="keyword token">function</span><span class="punctuation token">(</span>a<span class="punctuation token">,</span> b<span class="punctuation token">)</span> <span class="punctuation token">{</span>
- <span class="keyword token">var</span> nameA <span class="operator token">=</span> a<span class="punctuation token">.</span>name<span class="punctuation token">.</span><span class="function token">toUpperCase</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// ignore upper and lowercase</span>
- <span class="keyword token">var</span> nameB <span class="operator token">=</span> b<span class="punctuation token">.</span>name<span class="punctuation token">.</span><span class="function token">toUpperCase</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// ignore upper and lowercase</span>
- <span class="keyword token">if</span> <span class="punctuation token">(</span>nameA <span class="operator token">&lt;</span> nameB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
- <span class="keyword token">return</span> <span class="operator token">-</span><span class="number token">1</span><span class="punctuation token">;</span>
- <span class="punctuation token">}</span>
- <span class="keyword token">if</span> <span class="punctuation token">(</span>nameA <span class="operator token">&gt;</span> nameB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
- <span class="keyword token">return</span> <span class="number token">1</span><span class="punctuation token">;</span>
- <span class="punctuation token">}</span></code>
-
- <span class="comment token">// names must be equal</span>
- <span class="keyword token">return</span> <span class="number token">0</span><span class="punctuation token">;</span>
-<span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span></pre>
+// sort by name
+items.sort(function(a, b) {
+ var nameA = a.name.toUpperCase(); // ignore upper and lowercase
+ var nameB = b.name.toUpperCase(); // ignore upper and lowercase
+ if (nameA &lt; nameB) {
+ return -1;
+ }
+ if (nameA &gt; nameB) {
+ return 1;
+ }
+
+ // names must be equal
+ return 0;
+});</pre>
<h2 id="Examples" name="Examples">示例</h2>
@@ -214,6 +214,45 @@ var result = mapped.map(function(el){
});
</pre>
+<h3 id="sort_stability">排序稳定性</h3>
+
+<p>自 ES10(EcmaScript 2019)起,<a href="https://tc39.es/ecma262/#sec-array.prototype.sort">规范</a> 要求 <code>Array.prototype.sort</code> 为稳定排序。</p>
+<p>假设有一个包含学生名字和年级的列表,已经将它按学生名字字母顺序进行预排序:</p>
+<pre class="brush: js">const students = [
+ { name: "Alex", grade: 15 },
+ { name: "Devlin", grade: 15 },
+ { name: "Eagle", grade: 13 },
+ { name: "Sam", grade: 14 },
+];
+</pre>
+
+<p>对这个数组执行 <code>grade</code> 升序排序后:</p>
+
+<pre class="brush: js">students.sort((firstItem, secondItem) => firstItem.grade - secondItem.grade);
+</pre>
+
+<p><code>students</code> 变量如下:</p>
+
+<pre class="brush: js">[
+ { name: "Eagle", grade: 13 },
+ { name: "Sam", grade: 14 },
+ { name: "Alex", grade: 15 }, // grade 相同时维持原先的顺序 (稳定排序)
+ { name: "Devlin", grade: 15 }, // grade 相同时维持原先的顺序 (稳定排序)
+];
+</pre>
+
+<p>注意,那些年级相同的学生(如 Alex 和 Devlin)会维持调用排序之前的顺序,这是稳定排序所确保的。</p>
+
+<p>ES10(EcmaScript 2019)以前没有要求稳定性,意味着你可能会得到以下结果:</p>
+
+<pre class="brush: js">[
+ { name: "Eagle", grade: 13 },
+ { name: "Sam", grade: 14 },
+ { name: "Devlin", grade: 15 }, // 没有维持原先的顺序
+ { name: "Alex", grade: 15 }, // 没有维持原先的顺序
+];
+</pre>
+
<h2 id="Specifications" name="Specifications">规范</h2>
<table class="standard-table">