
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sorting contents of a string that holds integer values in Java
Problem Statement
Here we have a string that contains integer values, our task is to sort those integer values in ascending order.
For example, if the string is "31134", the sorted string should be "11334".
Solution
We can solve the above problem using multiple ways:
Using char array
We will use the toCharArray() method of the String class to convert the string into a character array. Then we will sort the character array using the Arrays.sort() method. Finally, we will convert the sorted character array back to a string.
Following are the steps to sort the contents of a string that holds integer values:
- Convert the string to a character array using the toCharArray() method.
- Then use parseInt() method to convert each character to an integer.
- Sort the character array using the Arrays.sort() method.
- Then, convert the sorted character array back to a string using the String constructor.
Example
Following is the Java program to sort the contents of a string that holds integer values using char array:
import java.util.Arrays; public class SortIntString{ public static void main(String[] args){ String str = "8576842"; char[] arr = str.toCharArray(); int[] intArr = new int[arr.length]; for(int i = 0; i < arr.length; i++){ intArr[i] = Integer.parseInt(String.valueOf(arr[i])); } Arrays.sort(intArr); System.out.print("Sorted string: " + Arrays.toString(intArr).replaceAll("[\[\], ]", "")); } }
Output
Following is the output of the above code:
Sorted string: 2456788
Using Stream API
We can also use the Stream API to sort the contents of a string that holds integer values. We will convert the string to a stream of characters, then sort the stream, and then get the sorted characters back into a string.
Following are the steps to sort the contents of a string that holds integer values using Stream API:
- Convert the string to a stream of characters using the chars() method.
- Sort the stream using the sorted() method.
- Convert the sorted stream back to a string using the collect() method.
Example
Following is the Java program to sort the contents of a string that holds integer values using Stream API:
import java.util.stream.Collectors; import java.util.stream.Stream; public class SortIntString { public static void main(String[] args) { String str = "8576842"; String sortedStr = Stream.of(str.split("")) .mapToInt(Integer::parseInt) .sorted() .mapToObj(String::valueOf) .collect(Collectors.joining()); System.out.println("Sorted string: " + sortedStr); } }
Output
Following is the output of the above code:
Sorted string: 2456788
Using Bubble Sort
In this method, we will use the Bubble Sort algorithm to sort the numbers in the string. In order to do this, we will first convert the string to a character array, then we will implement the Bubble Sort algorithm to sort the character array, and finally, we will convert the sorted character array back to a string.
Example
Following is the Java program to sort the contents of a string that holds integer values using Bubble Sort:
import java.util.Arrays; public class SortIntString { public static void main(String[] args) { String str = "8576842"; char[] arr = str.toCharArray(); bubbleSort(arr); String sortedStr = new String(arr); System.out.println("Sorted string: " + sortedStr); } public static void bubbleSort(char[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { // swap arr[j] and arr[j+1] char temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } }
Output
Following is the output of the above code:
Sorted string: 2456788