Does Collections.sort keep order on equal elements?

北城以北 提交于 2019-11-29 13:14:31

The code in your question will do what you need it to, since Collections.sort() does preserve the order of equal elements.

From the documentation:

This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

In other words, if the entries are ordered by date before the sort(), they will stay ordered by date within each category after the sort().

If you don't want to rely on the original ordering, you can easily extend your comparator to first compare the categories and then break ties using the dates.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!