我如何获得某些特定对象的改造响应并对它们进行排序?

How do i get a Retrofit Response of some particular Objects and sort them?

我正在使用 Retrofit,我得到的响应没有任何问题,我正在获取数据并且一切正常,但我只需要使用 projectType == "FOLDER" 获取项目,丢弃 projectType == "PROJECT"个。

我该如何处理?我还想根据创建日期对它们进行排序。

interface ApiInterface {


@GET("projects")
fun getData(@Header("secretKey") apiKey : String): Call<ProjectList>}

示例输出:

{
"projects": [
    {
        "id": "00001",
        "creation": 1611162020,
        "projectType": "FOLDER",
        "name": "3 - Aufträge",        
        "archived": false
    },
    {
        "id": "00002",
        "creation": 1611158408,          
        "projectType": "PROJECT",
        "name": "Unter",
        "archived": false
    },
    {
        "creation": 122234,
        "name": "4 - Aus",
        "id": "00003",           
        "projectType": "FOLDER",
        "archived": false
    }]}

您可以使用过滤功能轻松过滤您的项目,如下所示

pList.projects.filter{ it.projectType == "FOLDER" }

此外,如果您想根据 属性 对项目进行排序,您可以使用 sortedBysortedByDescending,如下所示

val ans = pList.projects
    .filter{ it.projectType == "FOLDER" }
    .sortedBy { it.position.creation }

反序

val ans = pList.projects
    .filter{ it.projectType == "FOLDER" }
    .sortedByDescending { it.position.creation }