Skip to content

Commit ec9271e

Browse files
author
vitaly.basaraba
committed
Featured: sort question
1 parent ea0110f commit ec9271e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

sort.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const path = require('path');
2+
3+
const data = require(path.resolve(__dirname, 'question.json'));
4+
5+
function sortAndFilterUniqueLinks(arr) {
6+
const seenLinks = new Set();
7+
8+
// Filter objects with unique links
9+
const filteredArr = arr.filter(obj => {
10+
if (obj.link && !seenLinks.has(obj.link)) {
11+
seenLinks.add(obj.link);
12+
return true;
13+
}
14+
return false;
15+
});
16+
17+
// Sort the filtered array by link alphabetically
18+
return filteredArr.sort((a, b) => {
19+
if (a.link < b.link) return -1;
20+
if (a.link > b.link) return 1;
21+
return 0;
22+
});
23+
}
24+
25+
const old = data.filter(({link}) => !link);
26+
const newData = sortAndFilterUniqueLinks(data.filter(({link}) => link));
27+
const work = [...old, ...newData];
28+
29+
console.log(JSON.stringify(work));

0 commit comments

Comments
 (0)