回答編集履歴
2
追加
test
CHANGED
@@ -39,3 +39,67 @@
|
|
39
39
|
}});
|
40
40
|
|
41
41
|
```
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
---
|
46
|
+
|
47
|
+
追加
|
48
|
+
|
49
|
+
要件が違うらしいので、とりあえずtinysortにかますまえに、検証用にサンプル関数を書いてみます。
|
50
|
+
|
51
|
+
```javascript
|
52
|
+
|
53
|
+
const comparer = (a,b) => {
|
54
|
+
|
55
|
+
const reg = /^([^\d]*)(\d+)(.*)$/;
|
56
|
+
|
57
|
+
const paramA = a.match(reg).slice(1,4);
|
58
|
+
|
59
|
+
const paramB = b.match(reg).slice(1,4);
|
60
|
+
|
61
|
+
console.log(paramA);
|
62
|
+
|
63
|
+
console.log(paramB);
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
if(paramA[0] !== paramB[0]) { return stringComparer(paramA[0], paramB[0]); }
|
68
|
+
|
69
|
+
const numA = Number(paramA[1]);
|
70
|
+
|
71
|
+
const numB = Number(paramB[1]);
|
72
|
+
|
73
|
+
if(numA !== numB) { return numA - numB; }
|
74
|
+
|
75
|
+
console.log("2");
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
return stringComparer(paramA[2], paramB[2]);;
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
const stringComparer = (a, b) => {
|
84
|
+
|
85
|
+
if (a < b) { return -1; }
|
86
|
+
|
87
|
+
if (a > b) { return 1; }
|
88
|
+
|
89
|
+
return 0;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
console.log(["2月","10月","9月","8月","12月"].sort(comparer));
|
94
|
+
|
95
|
+
console.log(["第2回目","第10回目","第9回目","第8回目","第12回目"].sort(comparer));
|
96
|
+
|
97
|
+
console.log(["A2","A10","A9","A8","A12"].sort(comparer));
|
98
|
+
|
99
|
+
console.log(["2月","10月","第3回目","ABC999の9", "ABC999の0","BBB31","1月"].sort(comparer));
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
この例では、AAA999の8とaaa999の7とAAA999の6が大文字小文字を無視して並ぶようにはなりません。いずれにしても明確な仕様を作ってもらわないことにはコードはかけません。
|
104
|
+
|
105
|
+
また、関数を使えばこういうことができる、というのが私の回答ですので、どういう関数を作るかは質問者様の仕様と技術でカバーしてください(仕様を提示してこのコード書いてというなら作業委託してください)
|
1
rep
test
CHANGED
@@ -1 +1,41 @@
|
|
1
1
|
optionとしてsortFunctionを指定できるようなので、月を消して数値化して比較するような関数を渡せばできると思います
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
まず↓でテストしてみてください(chromeでF12を押してconsoleで実行するなど)
|
6
|
+
|
7
|
+
```javascript
|
8
|
+
|
9
|
+
const comparer = (a,b) => {
|
10
|
+
|
11
|
+
const numA = Number(a.replace(/[^\d]/g, ''));
|
12
|
+
|
13
|
+
const numB = Number(b.replace(/[^\d]/g, ''));
|
14
|
+
|
15
|
+
return numA - numB;
|
16
|
+
|
17
|
+
}
|
18
|
+
|
19
|
+
console.log(["2月","10月","9月","8月","12月"].sort(comparer));
|
20
|
+
|
21
|
+
console.log(["第2回目","第10回目","第9回目","第8回目","第12回目"].sort(comparer));
|
22
|
+
|
23
|
+
console.log(["A2","A10","A9","A8","A12"].sort(comparer));
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
納得できたら、↓こういう感じに書いてください
|
28
|
+
|
29
|
+
```javascript
|
30
|
+
|
31
|
+
tinysort('ul#xcst>li',{sortFunction:function(a,b) {
|
32
|
+
|
33
|
+
const numA = Number(a.replace(/[^\d]/g, ''));
|
34
|
+
|
35
|
+
const numB = Number(b.replace(/[^\d]/g, ''));
|
36
|
+
|
37
|
+
return numA - numB;
|
38
|
+
|
39
|
+
}});
|
40
|
+
|
41
|
+
```
|