根据日期对JSON排序 [英] Sorting a JSON based on date

查看:67
本文介绍了根据日期对JSON排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据期间内的日期对json进行排序,但它还包含其他字符串(MAT TY和MAT YA)值,我们必须始终将它们保留在列表的最前面(首先是MAT TY,然后是MAT YA),排序后的项目,那我该怎么办?这是JSON响应:

I want to sort a json based on the date inside period but it also contains other string(MAT TY and MAT YA) values which we have to keep in the front of list always(MAT TY first then MAT YA after) and the sorted items after that so how do i do it? Here's the JSON response:

 [{

        "Period": "MAT YA",
        "StartUnit": null,
        "EndUnit": null,

    },
    {

        "Period": "DEC 2018",
        "StartUnit": null,
        "EndUnit": null,

    },
    {

        "Period": "NOV 2018",
        "StartUnit": null,
        "EndUnit": null,

    },
    {

        "Period": "SEP 2018",
        "StartUnit": null,
        "EndUnit": null,

    },
    {

        "Period": "JUN 2019",
        "StartUnit": null,
        "EndUnit": null,

    },
    {

        "Period": "MAT TY",
        "StartUnit": null,
        "EndUnit": null,
    },
    {

        "Period": "MAT YA",
        "StartUnit": null,
        "EndUnit": null,

    },
    {

        "Period": "MAT YA",
        "StartUnit": null,
        "EndUnit": null,

    },
    {

        "Period": "MAT TY",
        "StartUnit": null,
        "EndUnit": null,

    }
]

我知道这样的比较器:

myDateList.sortWith(Comparator { s1, s2 -> myDateList.indexOf(s1).compareTo(myDateList.indexOf(s2))})

那么我该如何转换它以满足以上要求?

So how can i convert it to meet the above requirement?

更新: 自定义比较器:

 private fun customSortFunction(mydateList: ArrayList<String>){
        val sdf = SimpleDateFormat("MMM yyyy")
        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, "Date list: "+ mydateList)
    }

StackTrace :

 java.lang.IllegalArgumentException: Comparison method violates its general contract!
        at java.util.TimSort.mergeHi(TimSort.java:899)
        at java.util.TimSort.mergeAt(TimSort.java:516)
        at java.util.TimSort.mergeCollapse(TimSort.java:441)
        at java.util.TimSort.sort(TimSort.java:245)
        at java.util.Arrays.sort(Arrays.java:1523)
        at java.util.Collections.sort(Collections.java:238)
        at kotlin.collections.CollectionsKt__MutableCollectionsJVMKt.sortWith(MutableCollectionsJVM.kt:34)
        at com.abc.ShareStoryFragment.customSortFunction(ShareStoryFragment.kt:429)

第429行是:

mydateList.sortWith(comparator)

Mydatelist值:

Mydatelist values:

[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]

请帮助.

推荐答案

比较逻辑描述了 B和B> A的情况.当两个字符串相同时,您需要另一个条件来处理:

The comparison logic described does not follow the contract. If there is no branch covering when both strings are "MAT YA" or "MAT TY", you can end up with situations where A > B and B > A. You need another condition up top that handles when both strings are the same:

private val val sdf = SimpleDateFormat("MMM yyyy")

fun compare(s1: String?, s2: String?): Int = when {
  s1 == s2 -> 0
  s1 == "MAT TY" -> -1
  s2 == "MAT TY" -> 1
  s1 == "MAT YA" -> -1
  s2 == "MAT YA" -> 1
  s1 == null -> 1
  s2 == null -> -1
  else -> sdf.parse(s1).compareTo(sdf.parse(s2))
}

这篇关于根据日期对JSON排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆