Matsushita's Blog

Sort an array of strings by anagrams


Write a method to sort an array of strings so that all the anagrams are next to each other.


This problem asks us to group the strings in arrays such that anagrams appear next to each other.

How to find the anagram

The feature of anagrams is that the sorted anagrams are equivalent. So we sort all strings and find the anagrams by HashMap. Putting the anagram as a key and setting the LinkedList as a value into HashMap, we can group the anagrams.

Source Code