public static List<List<String>> collect(String[] array) {
		if (array == null || array.length == 0)
			return null;
		
		int arrayLength = array.length;
		Data[] dataArray = new Data[arrayLength];
		
		// sort characters in each word, O(kn) time
		for (int i = 0; i < arrayLength; i++)
			dataArray[i] = new Data(sortCharacters(array[i]), i);
		
		// sort words, O(kn) time
		sortWords(dataArray, 0, arrayLength - 1, 0, findMaxLength(array));
		
		// collect anagrams in one pass
		List<List<String>> result = new ArrayList<List<String>>();
		String prev = null;
		for (int i = 0; i < arrayLength; i++) {
			if (!dataArray[i].word.equals(prev))
				result.add(new ArrayList<String>());
			result.get(result.size() - 1).add(array[dataArray[i].index]);
			prev = dataArray[i].word;
		}
		
		return result;
	}