-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?

5
  • 4
    what did you try so far? Commented Oct 29, 2016 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? Commented Oct 29, 2016 at 12:49
  • @Sparky: Ameya Sinah is asking about Java, not JavaScript. Commented Oct 29, 2016 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!): stackoverflow.com/questions/13335233/… stackoverflow.com/questions/12986386/… Commented Oct 29, 2016 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. Commented Oct 30, 2016 at 10:23

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

1 Comment

All right thanks this is what i wanted to know :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.