-2

For example I have an array of 5 elements and i want to sort only elements 1-3??

array={"abc","rst","pqr","qwerty","lmn"}

my array should be array={"abc","pqr","qwerty","rst","lmn"} How do i proceed?

  • 4
    what did you try so far? – PKR Oct 29 '16 at 12:09
  • Following up on what PKR said... The hover-text for the downvote button begins, "This question does not show any research effort...". If you put no effort into this, why should we? – Kevin J. Chase Oct 29 '16 at 12:49
  • @Sparky: Ameya Sinah is asking about Java, not JavaScript. – Kevin J. Chase Oct 29 '16 at 17:38
  • (Thanks to @KevinJ.Chase) You might want to look at the following discussions about sorting arrays of strings, and sorting subsets of arrays of strings in Java (not JavaScript!): http://stackoverflow.com/questions/13335233/sorting-subset-of-string-array-in-java-ignoring-case http://stackoverflow.com/questions/12986386/sorting-an-array-of-strings-with-java – Marcus Oct 29 '16 at 18:59
  • I wanted to know if there is a function that could do it. I knew about Arrays.sort but not that it accepts parameters. I had googled it but did not find anything substantial. – Ameya Sinha Oct 30 '16 at 10:23

1 Answers1

2

Arrays.sort accepts range parameters.

Example sorting array indexes 1 until 3 inclusive:

String[] arr = {"abc", "rst", "pqr", "qwerty", "lmn"};
Arrays.sort(arr, 1, 4);
System.out.println(Arrays.toString(arr));
janos
  • 120,954
  • 29
  • 226
  • 236