Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an ArrayList<String> based on specific index range

My need is to sort the ArrayList of Strings, based on specific index range. For example I have following items in list: ["abc", "xyz", "pqr" , "asd"]Now I want to sort this list from index 1 till last index.

One way As I think that I can create a sub-list from main list with desired index range, sort it and add the sub-list accordingly. But my question is:

Is there any API already available for that? Or any other faster way to achieve this.

like image 835
Anand Avatar asked Aug 29 '11 13:08

Anand


1 Answers

You should do

Collections.sort(yourList.subList(1, yourList.size()));

Since the List.subList method returns a view of the list, modifications done by Collections.sort will affect the backing list as well.

like image 195
aioobe Avatar answered Oct 12 '22 12:10

aioobe