Java Program to sort a subset of array elements

Java 8Object Oriented ProgrammingProgramming

Let us first create a string array −

String[] strArr = new String[] { "r", "p", "v","y", "s", "q" };

Now, use Arrays.sort() to get the subset. Use the following to sort only from the index range 2 to 6.

Arrays.sort(strArr, 2, 6);

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String[] args) {
      String[] strArr = new String[] { "r", "p", "v","y", "s", "q" };
      Arrays.sort(strArr, 2, 6);
      System.out.println("Sorted subset of array elements from index 2 to 6...");
      for (int a = 0; a < strArr.length; a++) {
         System.out.println(strArr[a]);
      }
   }
}

Output

Sorted subset of array elements from index 2 to 6...
r
p
q
s
v
y
raja
Published on 23-Apr-2019 12:34:37
Advertisements