Last active
July 9, 2023 00:57
-
-
Save xer0x/caf82df9d1fffbed580d502aefab76ec to your computer and use it in GitHub Desktop.
Count occurrences in a list, and sort output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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