I have a set of intervals S contained in an array list. How can I sort those arrays based on the left or right end-points?
For instance, if I had the following intervals [1,3],[1,2],[2,5],[3,3] how can I sort them based on their left end-points?
I am aware of the different sorting algorithms like sequential sort, insertion sort and merge sort, but I'm clueless on how to sort a set of intervals.
Any help or hints to guide me in the right direction will be appreciated.
尝试基数排序,如果所有值都是一位数,它会帮助你,它很容易实现。
Try with this command
intervals.sort((i1, i2) -> Integer.compare(i1.start, i2.start));
Hopefully you will get it correct based on starting points or if you want to sort based on end points just compare (i1.end,i2.end)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.