0

hey i want to sort item from the new to the old by id from json i want the new item to apear on the top of the list i am using this code

try {
    JSONObject mainJson = new JSONObject(result);
    JSONArray jsonArray = mainJson.getJSONArray(Constant.ARRAY_NAME);
    JSONObject objJson = null;

    for (int i = 0; i < jsonArray.length();  i++) {
        objJson = jsonArray.getJSONObject(i);

        ItemWallpaper objItem = new ItemWallpaper();

        objItem.setWallId(objJson.getString(Constant.WALL_ID));
        objItem.setWallImage(objJson.getString(Constant.WALL_IMAGE));

        arrayOfLatestImage.add(objItem);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

for(int j=0;j<arrayOfLatestImage.size(); j++)
{
    objAllBean=arrayOfLatestImage.get(j);

    allListImage.add(objAllBean.getWallImage());
    allArrayImage=allListImage.toArray(allArrayImage);

    allListId.add(objAllBean.getWallId());
    allArrayId=allListId.toArray(allArrayId);
 }
beeb
  • 1,615
  • 1
  • 12
  • 24

2 Answers2

1

I don't fully understand your code. But i assume you just want the arrayOfLatestImage sorted by the wallId of the ItemWallpaper objects.

The keyword you are looking for is "comperator" in java. small example taken from Java Comparator class to sort arrays

Arrays.sort(twoDim, new Comparator<int[]>() {
  @Override
  public int compare(int[] o1, int[] o2) {
      return ((Integer) o2[0]).compareTo(o1[0]);
  }
});

Check here for more information https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

Timmeey
  • 363
  • 3
  • 9
0

Just write an own comparator which compares the ItemWallpaper objects. Check this link for more info: https://developer.android.com/reference/java/util/Comparator.html

beeb
  • 1,615
  • 1
  • 12
  • 24