Skip to content

Instantly share code, notes, and snippets.

@xer0x
Last active July 9, 2023 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xer0x/caf82df9d1fffbed580d502aefab76ec to your computer and use it in GitHub Desktop.
Save xer0x/caf82df9d1fffbed580d502aefab76ec to your computer and use it in GitHub Desktop.
Count occurrences in a list, and sort output.
// Read the list, and count the names after the @ symbols.
// current-history.txt format:
// @user1 @user2
const fs = require('fs');
const history = {};
const text = fs.readFileSync('current-history.txt', 'utf8');
const lines = text.split('\n');
for (const line of lines) {
const people = Array.from(line.matchAll(/(@\w+\s*\w*)/g));
people.forEach(([person]) => {
history[person] = history[person] ? history[person] + 1 : 1
});
}
sortedPeople = Object.entries(history).sort((a, b) => {
return a[1] - b[1];
}).reverse();
const listAsText = sortedPeople.map(person => person.reverse().join(' ')).join('\n');
console.log(listAsText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment