-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortWords.java
More file actions
35 lines (29 loc) · 1001 Bytes
/
Copy pathSortWords.java
File metadata and controls
35 lines (29 loc) · 1001 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
public class SortWords {
public static void main(String[] args) {
String str = "is2 sentence4 This1 a3";
String[] arr = str.split(" ");
int[] num = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
num[i] = (int) arr[i].charAt(arr[i].length() - 1);
}
// Sort
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (num[j] > num[j + 1]) {
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
// arr comparison
String t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
String res = "";
for (int i = 0; i < arr.length; i++) {
res = res + arr[i].substring(0, arr[i].length() - 1) + " ";
}
System.out.println(res);
}
}