2

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?

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
Ivan Salosin
  • 343
  • 1
  • 2
  • 8

1 Answers1

3

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

Aubin
  • 14,617
  • 9
  • 61
  • 84
Zeeshan Bilal
  • 1,147
  • 1
  • 8
  • 22