Here's my String Arraylist:
[SEP 2019, JUL 2019, AUG 2019, JUN 2019, MAY 2019, APR 2019, MAT TY, MAR 2019, FEB 2019, MAT YA, NOV 2018, SEP 2018, DEC 2018, OCT 2018, JAN 2019, FEB 2019, SEP 2018, JAN 2019, NOV 2018, OCT 2018, DEC 2018, MAT YA, MAT YA, NOV 2018, MAR 2019, MAT TY, SEP 2018, OCT 2018, DEC 2018, APR 2019, JUL 2019, APR 2019, SEP 2019, AUG 2019, MAY 2019, AUG 2019, MAT TY, JAN 2019, FEB 2019, JUN 2019, MAR 2019, MAY 2019, SEP 2019, JUL 2019, JUN 2019, APR 2019, MAR 2019, FEB 2019, MAY 2019, JAN 2019, MAT YA, MAT TY, AUG 2019, DEC 2018, SEP 2019, JUL 2019, NOV 2018, JUN 2019, SEP 2018, OCT 2018, SEP 2019, AUG 2019, JUL 2019, MAY 2019, APR 2019, JUN 2019, MAT TY, MAR 2019, FEB 2019, MAT YA, DEC 2018, OCT 2018, NOV 2018, JAN 2019, SEP 2018, MAT YA, SEP 2018, DEC 2018, OCT 2018, NOV 2018, FEB 2019, JAN 2019, MAR 2019, APR 2019, MAT TY, JUL 2019, MAY 2019, JUN 2019, SEP 2019, AUG 2019, JUN 2019, AUG 2019, SEP 2019, OCT 2018, JUL 2019, SEP 2018, MAY 2019, MAT TY, JAN 2019, NOV 2018, DEC 2018, FEB 2019, MAR 2019, APR 2019, MAT YA]
Custom Comparator method:
private fun customSortFunction(mydateList: ArrayList<String>){
val sdf = SimpleDateFormat("MMM yyyy")
Log.e(TAG, "My date list: "+mydateList)
val stringList = arrayListOf(
"JUN 2019",
"MAT YA",
"SEP 2018",
"MAT YA",
"MAT TY",
"NOV 2018"
)
val comparator = Comparator<String> { date1, date2 ->
if (date1 == "MAT TY") return@Comparator -1
if (date2 == "MAT TY") return@Comparator 1
if (date1 == "MAT YA") return@Comparator -1
if (date2 == "MAT YA") return@Comparator 1
val date1Formatted = sdf.parse(date1)
val date2Formatted = sdf.parse(date2)
return@Comparator date1Formatted.compareTo(date2Formatted)
}
mydateList.sortWith(comparator)
Log.e(TAG, "String list is: "+mydateList)
println(mydateList)
}
The above method of sorting the the above arraylist works fine for the arraylist size<101 means 100 but not after that. It gives the error of:
"java.lang.IllegalArgumentException: Comparison method violates its general contract!"
I am unable to find its reason and also not able to convert it to accept the arraylists sized > 100. Please write your opinions and solution what are we doing wrong here?
This problem came while "Christilyn" provided me a solution to solve my sorting problem in this SO thread Sorting the arraylist datewise