how to sort and reverse a string in java?

Program to sort and reverse a string in java, there are following way :

Using append method:-

    • we pass String character in Arrays sort method.now make one StringBuilder class object.

    • Arrays is the class defined in the java.util package that provides sort() method to sort an array in ascending order.

    • Now call for each method pass this character to String.

    • From append method we store this String in StringBuilder .

    • After that call StringBuilder reverse method and print data.

    • The descending order arranges the elements in the highest to lower order.

               Syntax:-   public static void sort(int[] a)

Using for loop:-

    • We create one for loop in reverse order.

    • Apply for loop to get the characters of the st in the reverse order i.e int i = st.length()-1; i >= 0; i- -;

    • Inside the for loop append each character with the reversedString.

    • Print the reversedString.

				
					import java.util.Arrays;

public class SortCharDesc {
	public static void main(String[] args) {
		String[] st = { "a", "b", "r", "q", "c" };
		Arrays.sort(st);
		StringBuilder d = new StringBuilder();
		for (String h : st) {
			d.append(h);
		}
		d.reverse();
		System.out.println("using append reverse method:- " + d);

		String reverseString = "";
		for (int i = st.length - 1; i >= 0; i--) {
			reverseString = reverseString + st[i];
		}
		System.out.println("for loop in reverse order:- " + reverseString);

		String str = "welcome";
		char ch[] = str.toCharArray();
		StringBuilder sb = new StringBuilder();
		sb.append(ch);
		sb.reverse();
		System.out.println("using append and toCharArray method:- " + sb);
	}
}
				
			

output:-
using append reverse method:- rqcba
for loop in reverse order:- rqcba
using append and toCharArray method:- emoclew