How to sort the string in java?
string sort in java
package com.tech.mindclues; public class SortString { public static void main(String args[]) { String original = "rajeev"; int j = 0; char temp = 0; System.out.println("Original String : " + original); char[] chars = original.toCharArray(); for (int i = 0; i < chars.length; i++) { for (j = 0; j < chars.length; j++) { if (chars[j] > chars[i]) { temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } } } System.out.println("Sorted string :"); for (int k = 0; k < chars.length; k++) { System.out.println(chars[k]); } } }
Original String : rajeev Sorted string : a e e j r v
post a comment