jkulvich

One-line grouping by anagram

Jun 20th, 2021
651
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. (words => {
  3.     'use strict'
  4.     if (!words || !Array.isArray(words) || !words.every(word => typeof (word) === typeof (String()))) return null
  5.     return Object.values((wordsMap => words.map(word => [word, word.split('').sort().join('')]).forEach(([word, hash]) => wordsMap[hash] ? wordsMap[hash].push(word) : wordsMap[hash] = [word]) || wordsMap)(new Set())).filter(wordsGroup => wordsGroup.length > 1).map(wordsGroup => wordsGroup.sort()).sort()
  6. })(['стоп', 'ток', 'кошка', 'пила', 'коробка', 'кот', 'барокко', 'пост', 'липа'])
RAW Paste Data