回答編集履歴

4

修正

2020/02/21 17:34

投稿

kairi003
kairi003

スコア1332

test CHANGED
@@ -19,3 +19,27 @@
19
19
  console.log(list);
20
20
 
21
21
  ```
22
+
23
+
24
+
25
+ 追記
26
+
27
+ すみません、-Infinityから-Infinityを引くとNaNになっちゃいますね…
28
+
29
+ やはり
30
+
31
+ ```javascript
32
+
33
+ var comp = function(a, b) {
34
+
35
+ if (a[1] === "") return 1;
36
+
37
+ if (b[1] === "") return -1;
38
+
39
+ return (b[1] - a[1]);
40
+
41
+ }
42
+
43
+ ```
44
+
45
+ が妥当でしょうか

3

修正

2020/02/21 17:34

投稿

kairi003
kairi003

スコア1332

test CHANGED
@@ -14,10 +14,8 @@
14
14
 
15
15
  }
16
16
 
17
-
18
-
19
17
  var list = [["aaa",100],["bbb",10],["ccc",""],["ddd",30],["eee",40]].sort(comp);
20
18
 
21
-
19
+ console.log(list);
22
20
 
23
21
  ```

2

修正

2020/02/20 13:07

投稿

kairi003
kairi003

スコア1332

test CHANGED
@@ -18,4 +18,6 @@
18
18
 
19
19
  var list = [["aaa",100],["bbb",10],["ccc",""],["ddd",30],["eee",40]].sort(comp);
20
20
 
21
+
22
+
21
23
  ```

1

修正

2020/02/20 13:06

投稿

kairi003
kairi003

スコア1332

test CHANGED
@@ -4,20 +4,18 @@
4
4
 
5
5
  ```javascript
6
6
 
7
- function testSort(list) {
7
+ var comp = function(a, b) {
8
8
 
9
- list.sort(function(a, b) {
9
+ var _a = (a[1] === "") ? -Infinity : a[1];
10
10
 
11
- var _a = (a[1] === "") ? -Infinity : a[1];
11
+ var _b = (b[1] === "") ? -Infinity : b[1];
12
12
 
13
- var _b = (b[1] === "") ? -Infinity : b[1];
14
-
15
- return (_b - _a);
13
+ return (_b - _a);
16
-
17
- });
18
-
19
- return list;
20
14
 
21
15
  }
22
16
 
17
+
18
+
19
+ var list = [["aaa",100],["bbb",10],["ccc",""],["ddd",30],["eee",40]].sort(comp);
20
+
23
21
  ```