-3

can you sort list by time added? Ex.: I add to list "Some user" and then I add "Second user" and "Random Guy". If I will sort with Collections.sort(), "Random user" will be first, "Second user" second and "Some user" third, but I need make "Some user" will be first, "Second user" second and "Random guy" last...

ENGO_150
  • 17
  • 5
  • 3
    Why do you use ```Collections.sort()``` if you want to keep the initial order? Just drop that and you will be fine. – flappix May 06 '20 at 09:22
  • I am not 100% sure what the problem is (may you could add some more information, like the code you are starting with). Maybe this helps: You can use custom comparators for sorting. Please see this post, https://stackoverflow.com/questions/5245093/how-do-i-use-comparator-to-define-a-custom-sort-order, and also the documentation of Collections.sort: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html – Dirk R May 06 '20 at 09:24
  • What happens if you try sorting using `Collections.reverseOrder()` or `Collections.reverse` just to revert the list? – Nowhere Man May 06 '20 at 18:42

1 Answers1

1

Guideline - You need to have proper formatting and explanation of your question so that its easy to understand and also let us know what you have already tried.

Solution - ArrayList in java already maintains the order of insertion. So the item inserted first will be at first index.

If you want to sort based on time or any other attribute you can check this reference - https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/

Jitesh Shivnani
  • 363
  • 1
  • 9