Do Collections.sort
on its subList
instead.
List<String> list = new ArrayList<String>(
Arrays.asList("Zzz...", "Two", "One", "Three")
);
Collections.sort(list.subList(1, list.size()));
System.out.println(list);
// "[Zzz..., One, Three, Two]"
API links
subList(int fromIndex, int toIndex)
- Returns a view of the portion of this list between the specified
fromIndex
, inclusive, and toIndex
, exclusive. The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.
If the special element is not at index 0, then simply put it there before you sort it as follows:
List<String> list = new ArrayList<String>(
Arrays.asList("Four", "Five", "Zzz...", "Two", "One", "Three")
);
Collections.swap(list, list.indexOf("Zzz..."), 0);
Collections.sort(list.subList(1, list.size()));
System.out.println(list);
// "[Zzz..., Five, Four, One, Three, Two]"
API links