如何排序从Android的JSON对象JSON阵列? [英] How to Sort JSON Array from JSON Objects in Android?

查看:454
本文介绍了如何排序从Android的JSON对象JSON阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON数组,我想对它进行排序根据不同的名称和字母顺序也。
我的JSON是如下:

I have a JSON array, and I want to sort it Depending on the name and too alphabetically. My Json is as follows.

[
    {
        "UserID": 77,
        "InvID": 333,
        "Email": "sumeetssm@gmail.com",
        "Phone": "",
        "Name": "Summet",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 334,
        "Email": "amit@testt.com",
        "Phone": "",
        "Name": "Amit",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 3,
        "InvID": 335,
        "Email": "amitesh@yahoo.com",
        "Phone": "",
        "Name": "Amitesh",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 336,
        "Email": "foobar@test.com",
        "Phone": "",
        "Name": "FOO",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 337,
        "Email": "krazy.lance@gmail.com",
        "Phone": "",
        "Name": "Krazy",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 1,
        "InvID": 338,
        "Email": "test@yahoo.com",
        "Phone": "",
        "Name": "Test",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 339,
        "Email": "maktest@yahoo.com",
        "Phone": "",
        "Name": " ",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 340,
        "Email": "Sam@rediff.com",
        "Phone": "",
        "Name": "SAM",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 101,
        "InvID": 343,
        "Email": "anurag@yahoo.com",
        "Phone": "",
        "Name": "Anurag",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 95,
        "InvID": 379,
        "Email": "s@s.com",
        "Phone": "",
        "Name": "S",
        "Company": "",
        "Date": "2014-01-14T00:00:00"
    },
    {
        "UserID": 0,
        "InvID": 428,
        "Email": "sumeetssm@yahoo.com",
        "Phone": "",
        "Name": "Summet",
        "Company": "",
        "Date": "2014-01-16T00:00:00"
    },
    {
        "UserID": 4,
        "InvID": 494,
        "Email": "sameer@iconnectgroup.com",
        "Phone": "",
        "Name": "Sameer P",
        "Company": "",
        "Date": "2014-01-21T00:00:00"
    },
    {
        "UserID": 85,
        "InvID": 507,
        "Email": "jonny@dep.com",
        "Phone": "9404988188",
        "Name": "Jonny Dep",
        "Company": "Iconnect",
        "Date": "2014-01-22T00:00:00"
    }
]

正如你可以看到我有一个键值名称,并在我得到尊敬的名字。我要排序完全基于这个名字这个JSON数组,而且也alphabetically.Thanks提前。

As you can see I have a key value "Name" and in that I get respected names. I want to sort this Json array completely based on this names and that too alphabetically.Thanks in advance.

推荐答案

转换 JSONArray 的ArrayList< JSONBbject> 并利用集合排序ArrayList中

Convert JSONArray to Arraylist<JSONBbject> and use Collections to sort your arraylist

例如:

ArrayList<JSONObject> array = new ArrayList<JSONObject>();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
    try {
        array.add(jsonArray.getJSONObject(i));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Collections.sort(array, new Comparator<JSONObject>() {

    @Override
    public int compare(JSONObject lhs, JSONObject rhs) {
        // TODO Auto-generated method stub

        try {
            return (lhs.getString("Name").toLowerCase().compareTo(rhs.getString("Name").toLowerCase()));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }
});

这篇关于如何排序从Android的JSON对象JSON阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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