-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBigSorting.java
More file actions
48 lines (40 loc) · 1019 Bytes
/
BigSorting.java
File metadata and controls
48 lines (40 loc) · 1019 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
36
37
38
39
40
41
42
43
44
45
46
47
48
// https://www.hackerrank.com/challenges/big-sorting/problem
import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
class BigSorting {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] unsorted = new String[n];
for(int i = 0; i < n; i++) {
unsorted[i] = in.next();
}
Arrays.sort(unsorted, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return StringAsIntegerCompare(a, b);
}
});
for(int i = 0; i < n; i++) {
System.out.println(unsorted[i]);
}
}
static int StringAsIntegerCompare(String s1, String s2) {
if(s1.length() > s2.length()) {
return 1;
}
if(s1.length() < s2.length()) {
return -1;
}
for(int i = 0; i < s1.length(); i++) {
if((int)s1.charAt(i) > (int)s2.charAt(i)) {
return 1;
}
if((int)s1.charAt(i) < (int)s2.charAt(i)) {
return -1;
}
}
return 0;
}
}