如何对字符串ArrayList进行排序?

时间:2014-06-10 03:49:08

标签: java android json bubble-sort

我正在成功解析JSON数组。但我有一个有数字的字符串。所以我想根据数字对所有数据进行排序。我已经检查了很多例子但我无法在我的代码中实现它们。所以请帮助我。 这是我的代码。这里的“count”是,我想要对数据进行排序的字符串。

a.java

    @Override
    protected Void doInBackground(Void... params) {
        ServiceHandler serviceHandler = new ServiceHandler();
        String jsonStr = serviceHandler.makeServiceCall(
                JSONUrl.categoriesUrl, ServiceHandler.GET);
        Log.d("Response Categories:", ">" + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                categoriesJSONArray = jsonObj
                        .getJSONArray(JSONUrl.TAG_DATA);

                for (int i = 0; i < categoriesJSONArray.length(); i++) {
                    JSONObject c = categoriesJSONArray.getJSONObject(i);

                    GridViewItem gridCategoriesItem = new GridViewItem();
                    gridCategoriesItem.setSlug(c
                            .getString(JSONUrl.TAG_CATEGORIES_SLUG));
                    gridCategoriesItem.setImage(c
                            .getString(JSONUrl.TAG_CATEGORIES_IMAGE));
                    gridCategoriesItem.setCount(c
                            .getString(JSONUrl.TAG_CATEGORIES_COUNT));

                    mGridArrayCategories.add(gridCategoriesItem);

                    }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.d("ServiceHandler", "Couldn't get any data from the url");
        }
        return null;
    }

GridViewItem.java

public class GridViewItem {
String image;
String slug;
String count;
String name;

public GridViewItem() {
    super();
}

public GridViewItem(String image, String slug, String count,
        String name) {
    super();
    this.image = image;
    this.slug = slug;
    this.count = count;
    this.name = name;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getSlug() {
    return slug;
}

public void setSlug(String slug) {
    this.slug = slug;
}

public String getCount() {
    return count;
}

public void setCount(String count) {
    this.count = count;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

2 个答案:

答案 0 :(得分:3)

而不是使用o(n2)的冒泡排序。 您可以使用Collection类

中的本机排序

示例:

Collections.sort(mGridArrayCategories, new Comparator<GridViewItem>() {

    public int compare(GridViewItem s, GridViewItem s2) {
        return Integer.parseInt(s2.getCount()) - Integer.parseInt(s.getCount()); //this will sort your arrayList in decending order
    }
});

您需要将string解析为int,以便根据count

对其进行排序

答案 1 :(得分:1)

您可以使用Collection.sort()

用于排序任何类型的ArrayLsit对象。

 Collections.sort(listOfStringArrays,new Comparator<String[]>() {
            public int compare(String[] strings, String[] otherStrings) {
                return strings[1].compareTo(otherStrings[1]);
            }
        });