By the way this not the Home Work this is the question i have faced in one interview. I have tried the comparator , comparable interface along with the Collections.sort(),Arrays.sort(). just i want to know is there any implicit methods to do this sorting that's it .
Welcome to javaranch Karol. It will be easier to write this using a Comparable or Comparator implementation.
Joined: Oct 17, 2007
using Comparable , Comparator interface for sorting String data type alone (not a String Variable of Some Class) is not an efficient way i think because
1) Data Saved in the ArrayList is String data type.
2) If we want to use Comparable , Comparator interface we need to create Class for that
3) Resource Usage will go high if the size of the List Increase than the Karol Koranecki Coding
so i prefer Karol Kornecki method great work, keep it up
Which is the case regardless of whether you use Comparable or not.
If we want to use Comparable , Comparator interface we need to create Class for that
The memory overhead of an extra class is negligible and can be safely ignored.
Resource Usage will go high if the size of the List Increase than the Karol Koranecki Coding
How did you conclude this without knowing how a Comparable implementation sorts its elements ?
The Comparable and Comparator implementations define clear contracts that code should follow. I would expect a candidate to choose an existing solution / design over writing something anew. This is especially true with this scenario. You cannot use Comparable with Strings. Strings already have a natural ordering defined by Comparable. Comparator is a good answer, unless there was a requirement explicitly asking you not to use a Comparator.