Java Program For Case Specific Sorting Last Updated : 04 Sep, 2021 Comments Improve Suggest changes Like Article 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 Comment More infoAdvertise with us Next Article Java Program For Case Specific Sorting A AakashSingh_17 Follow Improve Article Tags : Strings Java Data Structures Java Programs DSA +1 More Practice Tags : Data StructuresJavaStrings Similar Reads Java Program for Counting Sort Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Java // Java implementation of Counting So 2 min read Java Program for QuickSort Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of QuickSort that pick pivot in different ways.Always pick first element as pivot.Always pick last element as pivot (im 2 min read Java Program for Menu Driven Sorting of Array In Java, sorting an array consists of arranging the elements in a particular order, such as ascending or descending. This can be achieved using various algorithms like Bubble Sort, Selection Sort, or Insertion Sort. A menu-driven program allows users to select the desired sorting method dynamically. 7 min read Java Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0, 6 min read Java Program to Sort Items By Weight Given two array items and weights which denotes items and their respective weights. Both arrays are of equal length. For every index 'i', items[i] and weights[i] represent the ith item name and its weight respectively. Your task is to return a new array of items sorted in decreasing order by their w 3 min read Java Program for Linear Search Linear Search is the simplest searching algorithm that checks each element sequentially until a match is found. It is good for unsorted arrays and small datasets.Given an array a[] of n elements, write a function to search for a given element x in a[] and return the index of the element where it is 2 min read Java Program to Sort LinkedList using Comparable In Java, LinkedList is a part of the collection framework provided in java.util package. LinkedList is a linear data structure where all the elements are unsorted in contiguous memory locations. The advantage of LinkedList is it is dynamic and easy to insert and delete any element. We can not access 5 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Program to Sort Objects in ArrayList by Date The foremost tool that strikes is the sort() method to be used for the comparator mechanism of the Collections class which sorts in the decreasing order. Yes if in generic we want to achieve the goal considering the boundary condition where objects to sorted are user-defined then blindly do with Com 6 min read Sort Java Vector in Descending Order Using Comparator The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implements the List interface, so we can use all the methods of the List interface. There are two types of Sorting t 3 min read Like