-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq10p2_1.java
More file actions
47 lines (45 loc) · 1.35 KB
/
Copy pathq10p2_1.java
File metadata and controls
47 lines (45 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class q10p2_1{
public static void main(String[] args){
String[] test = {"1234", "3214", "abc", "d", "bca", "4321", "bac", "d"};
Arrays.sort(test, new AnagramComparator());
//sort(T[] a, Comparator<? super T> c)
for(String str : test){
System.out.println(str);
}
}
/*
public static void sort2(String[] array){
HashMapList<String, String> mapList = new HashMapList<>();
for(String s: array){
String key = sortChars(s);
mapList.put(key, s);
}
int index = 0;
for(String key : mapList.keySet()){
ArrayList<String> list = mapList.get(key);
for(String t: list){
array[index] = t;
index++;
}
}
}
public static String sortChars(String s){
char[] content = s.toCharArray();
Arrays.sort(content);
return new String(content);
}
*/
}
//sort function in Arrays class bases on special comparator
class AnagramComparator implements Comparator<String>{
public String sortChars(String s){
char[] content = s.toCharArray();
Arrays.sort(content);
return new String(content);
}
@Override
public int compare(String s1, String s2){
return sortChars(s1).compareTo(sortChars(s2));
}
}