开发者

Sorting a part of Java ArrayList

开发者 https://www.devze.com 2023-02-14 02:44 出处:网络
What is the most efficient way of sorting only a part of ArrayList? Say all elements from index 0 to 3 in an Arraylist which contains 10 elements.

What is the most efficient way of sorting only a part of ArrayList? Say all elements from index 0 to 3 in an Arraylist which contains 10 elements.

Is there a library function available in Java?

Apart from Collections.sort(list) which sorts the entire List!

Writing a highly optimised c开发者_StackOverflowustom sort function will take some work.


Collections.sort(list.subList(0,3));

Note: '3' here is excluded from sorting

It is described in the documentation:

public List subList(int fromIndex, int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.


use the subList [inherited from AbstractList] method in ArrayList. And then use Collections.sort() on that sub-list. That is if writing a highly optimised custom sort function is truly hard work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号