Skip to content

Commit a7d376d

Browse files
新增 陣列物件欄位排序功能
1 parent f0d83ea commit a7d376d

File tree

6 files changed

+186
-9
lines changed

6 files changed

+186
-9
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aimidy/util",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"description": "",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",
@@ -30,6 +30,6 @@
3030
"ts-jest": "^29.1.2",
3131
"ts-node": "^10.9.2",
3232
"tsup": "^8.0.2",
33-
"typescript": "^5.3.3"
33+
"typescript": "^5.4.5"
3434
}
3535
}

src/help/fieldSorter/data.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
export const data1 = [
2+
{
3+
a: "10",
4+
b: "PHP",
5+
c: "A"
6+
},
7+
{
8+
a: "02",
9+
b: "PHP",
10+
c: "A"
11+
},
12+
{
13+
a: "03",
14+
b: "PHP",
15+
c: "A"
16+
},
17+
{
18+
a: "04",
19+
b: "PHP",
20+
c: "A"
21+
},
22+
{
23+
a: "05",
24+
b: "PHP",
25+
c: "A"
26+
},
27+
{
28+
a: "06",
29+
b: "PHP",
30+
c: "A"
31+
},
32+
{
33+
a: "07",
34+
b: "PHP",
35+
c: "A"
36+
},
37+
{
38+
a: "08",
39+
b: "PHP",
40+
c: "A"
41+
},
42+
{
43+
a: "09",
44+
b: "PHP",
45+
c: "A"
46+
},
47+
{
48+
a: "01",
49+
b: "PHP",
50+
c: "A"
51+
},
52+
{
53+
a: "11",
54+
b: "PHP",
55+
c: "A"
56+
},
57+
{
58+
a: "12",
59+
b: "PHP",
60+
c: "A"
61+
},
62+
];
63+
64+
export const data1Answer = [
65+
{
66+
a: "01",
67+
b: "PHP",
68+
c: "A"
69+
},
70+
{
71+
a: "02",
72+
b: "PHP",
73+
c: "A"
74+
},
75+
{
76+
a: "03",
77+
b: "PHP",
78+
c: "A"
79+
},
80+
{
81+
a: "04",
82+
b: "PHP",
83+
c: "A"
84+
},
85+
{
86+
a: "05",
87+
b: "PHP",
88+
c: "A"
89+
},
90+
{
91+
a: "06",
92+
b: "PHP",
93+
c: "A"
94+
},
95+
{
96+
a: "07",
97+
b: "PHP",
98+
c: "A"
99+
},
100+
{
101+
a: "08",
102+
b: "PHP",
103+
c: "A"
104+
},
105+
{
106+
a: "09",
107+
b: "PHP",
108+
c: "A"
109+
},
110+
{
111+
a: "10",
112+
b: "PHP",
113+
c: "A"
114+
},
115+
{
116+
a: "11",
117+
b: "PHP",
118+
c: "A"
119+
},
120+
{
121+
a: "12",
122+
b: "PHP",
123+
c: "A"
124+
},
125+
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { data1, data1Answer } from "./data";
2+
import { fieldSorter } from "./fieldSorter"
3+
4+
test("fieldSorter 測試 文字排序", () => {
5+
expect(data1.sort(fieldSorter({ fields: [{ key: "a" }] })))
6+
.toStrictEqual(data1Answer);
7+
})

src/help/fieldSorter/fieldSorter.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 多欄位排序
3+
* @param sort
4+
* @returns
5+
*/
6+
export function fieldSorter(sort: {
7+
isDesc?: boolean,
8+
fields: {
9+
key: string,
10+
isNumber?: boolean,
11+
isDesc?: boolean
12+
}[]
13+
}) {
14+
return (a: { [_: string]: any }, b: { [_: string]: any }) => {
15+
const data = sort.fields
16+
.map(filed => {
17+
let dir = 1;
18+
if (filed.isDesc) {
19+
dir = -1;
20+
}
21+
22+
let valueA = a[filed.key];
23+
let valueB = b[filed.key];
24+
25+
if (!valueA || !valueB)
26+
return 0;
27+
28+
if (filed.isNumber) {
29+
valueA = Number(valueA);
30+
valueB = Number(valueB);
31+
32+
if (Number.isNaN(valueA) || Number.isNaN(valueB))
33+
return 0
34+
}
35+
36+
return valueA > valueB ? dir : valueA < valueB ? -(dir) : 0;
37+
}).reduce((p, n) => p ? p : n, 0);
38+
39+
if (sort.isDesc)
40+
data * -1;
41+
42+
return data;
43+
};
44+
};

src/help/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export * from './remove/remove';
1010
export * from './searchInStr/searchInStr';
1111
export * from './strToConvert/strToConvert';
1212
export * from './strToHump/strToHump';
13-
export * from './deepAssign/deepAssign';
13+
export * from './deepAssign/deepAssign';
14+
export * from './fieldSorter/fieldSorter';

0 commit comments

Comments
 (0)