0

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

Cosmic Dev
  • 522
  • 6
  • 20
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `YearMonth` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 26 '19 at 09:16
  • No its working fine for arraylist size < 101 – Cosmic Dev Dec 26 '19 at 09:17
  • @OleV.V. Please if you can show some code – Cosmic Dev Dec 26 '19 at 09:18
  • I linked to the tutorial. There’s a lot of code there. Which Android API level are you developing for? Asking because java.time is built in from level 26. If targetting 25 or below, you will need to use [the backport, ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Dec 26 '19 at 09:24
  • Are you even reading the whole question? @OleV.V. If there is problem in my code then why it will run for 100 iterations? – Cosmic Dev Dec 26 '19 at 09:28
  • Yes, I had read the whole question. When your comparator violates the contract, the sorting method sometimes discovers this and throws the exception, in other case it doesn’t discover and hence doesn’t throw any exception. It discovers more often when sorting longer lists, which agrees with your observation. [The documentation of `List.sort()`](https://docs.oracle.com/javase/10/docs/api/java/util/List.html#sort(java.util.Comparator)) says: “**Throws** … `IllegalArgumentException` - (optional) if the comparator is found to violate the Comparator contract”. Note: **optional**. – Ole V.V. Dec 26 '19 at 09:39
  • So what's the needed correction in the above function? My target API is 27 but minimum is 17 – Cosmic Dev Dec 26 '19 at 09:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204870/discussion-between-ole-v-v-and-cosmic-dev). – Ole V.V. Dec 26 '19 at 09:43

0 Answers0