Skip to content

Commit dcec639

Browse files
committed
String sorting
1 parent 3472f28 commit dcec639

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.hellokoding.java.collections;
2+
3+
import java.util.Arrays;
4+
5+
public class StringSortDescendingByStringBuilder {
6+
static String sortAsc(String str) {
7+
// convert String to char array
8+
char[] arr = str.toCharArray();
9+
10+
// sort char array in alphabetically / ascending order
11+
Arrays.sort(arr);
12+
13+
// String join char array
14+
return String.valueOf(arr);
15+
}
16+
17+
static String sortDesc(String str) {
18+
return new StringBuilder(sortAsc(str)).reverse().toString();
19+
}
20+
21+
public static void main(String[] args) {
22+
String str = "bac";
23+
System.out.println(sortDesc(str));
24+
}
25+
}

0 commit comments

Comments
 (0)