I have an ArrayList
of something. I don't now correctly, but I'm pretty sure, that the Object
has the compareTo(Object)
, which returns int
in any cases. So, if I want to implement a sort(ArrayList)
, Objects may by strings, or integers.
private static void sort(ArrayList<String/Integers, or SomethingElse, not sure now> list)
I tried to search something like multi raw type, but I didn't find anything.
How should I do it?
If the List
consists of String
elements, it will be sorted into alphabetical order. If it consists of Date
elements, it will be sorted into chronological order. It will sort any any object/type that contain compareTo()
(implements Comparable
)
Reference: Collections.sort(list)
If you try to sort a list, the elements of which do not implement Comparable
, Collections.sort(list)
will throw a ClassCastException
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.