Skip to content

Commit

Permalink
Featured: sort question
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly.basaraba committed Jan 19, 2025
1 parent ea0110f commit ec9271e
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const path = require('path');

const data = require(path.resolve(__dirname, 'question.json'));

function sortAndFilterUniqueLinks(arr) {
const seenLinks = new Set();

// Filter objects with unique links
const filteredArr = arr.filter(obj => {
if (obj.link && !seenLinks.has(obj.link)) {
seenLinks.add(obj.link);
return true;
}
return false;
});

// Sort the filtered array by link alphabetically
return filteredArr.sort((a, b) => {
if (a.link < b.link) return -1;
if (a.link > b.link) return 1;
return 0;
});
}

const old = data.filter(({link}) => !link);
const newData = sortAndFilterUniqueLinks(data.filter(({link}) => link));
const work = [...old, ...newData];

console.log(JSON.stringify(work));

0 comments on commit ec9271e

Please sign in to comment.