-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexSort.java
More file actions
35 lines (28 loc) · 819 Bytes
/
LexSort.java
File metadata and controls
35 lines (28 loc) · 819 Bytes
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
/**Lexicographic sort*/
import java.util.Arrays;
import java.util.Comparator;
class LexCompare<T extends Comparable<T>> implements Comparator<T>{
@Override
public int compare(T x, T y){
if((x.getClass().getName() == "java.lang.Integer") && (y.getClass().getName() == "java.lang.Integer"))
return x.toString().compareTo(y.toString());
else
return -1;
}
}
public class LexSort{
public static void main(String[] args){
Integer[] A = {1, 2, 11, 100, 21, 23, 3, 300, 212, 24};
System.out.println("Array before sort:");
for(int i=0; i < A.length; i++){
System.out.print(A[i] + " ");
}
System.out.println();
Arrays.sort(A, new LexCompare());
System.out.println("Array after sort:");
for(int i=0; i < A.length; i++){
System.out.print(A[i] + " ");
}
System.out.println();
}
}