Sorting ArrayList

后端 未结 6 997
我在风中等你
我在风中等你 2021-01-25 19:19

Since I\'m just starting with JAVA, I\'m curious what is the best option for implementing sorting in JAVA (for ArrayLists). Below I provide my PHP code.

public i         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-25 20:04

    The simplest way to implement sorting in Java Collections is using the Collections#sort method.

    It uses a modified merge sort algorithm to do the job.

    It is important to say that it can only sort objects of a class that implements the Comparable interface, so you may need to take that into account. When you implement this interface you should know that it is best to think about the natural ordering of the object in question. e.g. Alphabetic order for Strings, if you need to sort it in an unnatural way on a specific context, do not use this interface.

    For that it's best if you define a Comparator when you invoke the method.

提交回复
热议问题