Java Program to sort a subset of array elements


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

Updated on: 30-Jul-2019

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements