Java Program For Case Specific Sorting Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report Given a string S consisting of uppercase and lowercase characters. The task is to sort uppercase and lowercase letters separately such that if the "i"th place in the original string had an Uppercase character then it should not have a lowercase character after being sorted and vice versa. it is described in the illustration as shown below: Illustration: Input : srbDKi Output: birDKsProcessing : After sorting we have to place the lowercase characters to the lowercase and uppercase character to specific uppercase character Approach: We will use two ArrayList one to store the lowercase values and the second to store uppercase values.After adding elements into the lists we will sort the list using Collections.sort(list) methodAfter sorting, we will traverse the string and check case-specific and store the element at the correct position Example: Java // Java Program for Case Specific Sorting // using Collections.sort(list) method // Importing input output classes // Importing utility classes import java.io.*; import java.util.*; class GFG { // Method 1 // To sort the string static String sortString(String str) { // Creating two Arraylist class objects // Declaring object of character type ArrayList<Character> list = new ArrayList<>(); ArrayList<Character> list2 = new ArrayList<>(); // Initially the string is empty String res = ""; // Iterating over the string for (int i = 0; i < str.length(); i++) { // Finding character at indexes // using charAt() method in List object if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') list.add(str.charAt(i)); if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') list2.add(str.charAt(i)); } // Sorting the Collection interface // using sort() method Collections.sort(list); Collections.sort(list2); int i = 0; int j = 0; // iterating over string using length() method for (int k = 0; k < str.length(); k++) { // If lowercase character encountered if (str.charAt(k) >= 'a' && str.charAt(k) <= 'z') { // Appending them all in beginning of string res += list.get(i); ++i; } // If uppercase character encountered else if (str.charAt(k) >= 'A' && str.charAt(k) <= 'Z') { // Appending them all together after // lowercase is finished in input string res += list2.get(j); ++j; } } return res; } // Method 2 // Main driver method public static void main(String[] args) { // Passing the custom string as input for which we // want case-specific sorting by calling the method // 1 as defined above System.out.println(sortString("defRTSersUXI")); } } OutputdeeIRSfrsTUX Create Quiz Comment A AakashSingh_17 Follow 1 Improve A AakashSingh_17 Follow 1 Improve Article Tags : Java Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like