2016-11-22 36 views
-2

我想根據時間排序兩個json數組流和stream1。如何按時間排序兩個json數組

了最新的時間會first.Time每個陣列中的記錄的格式像分鐘值前,幾天前

這是我的代碼至今:

parseJsonFeed(JSONObject response) { 
      try { 
       String stream = pref.getStream(); 
       JSONArray feedArray = response.getJSONArray(stream); 


       for (int i = 0; i < feedArray.length(); i++) { 
        JSONObject feedObj = (JSONObject) feedArray.get(i); 

        FeedItem item = new FeedItem(); 
        item.setId(feedObj.getInt("id")); 
        item.setName(feedObj.getString("title")); 

        // Image might be null sometimes 
        String image = feedObj.isNull("image") ? null : feedObj 
          .getString("image"); 
        item.setImge(image); 
        item.setStatus(feedObj.getString("status")); 
        item.setProfilePic(feedObj.getString("profilepic")); 
        item.setTimeStamp(feedObj.getString("created_at")); 

        // url might be null sometimes 
        String feedUrl = feedObj.isNull("url") ? null : feedObj 
          .getString("url"); 
        item.setUrl(feedUrl); 

        feedItems.add(item); 
       } 

       // notify data changes to list adapater 
       listAdapter.notifyDataSetChanged(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      String stream2 = pref.getStream2(); 

      if (!stream2.contentEquals("none")) { 

       try { 

        JSONArray feedArray = response.getJSONArray(stream2); 


        for (int i = 0; i < feedArray.length(); i++) { 
         JSONObject feedObj = (JSONObject) feedArray.get(i); 

         FeedItem item = new FeedItem(); 
         item.setId(feedObj.getInt("id")); 
         item.setName(feedObj.getString("title")); 

         // Image might be null sometimes 
         String image = feedObj.isNull("image") ? null : feedObj 
           .getString("image"); 
         item.setImge(image); 
         item.setStatus(feedObj.getString("status")); 
         item.setProfilePic(feedObj.getString("profilepic")); 
         item.setTimeStamp(feedObj.getString("created_at")); 

         // url might be null sometimes 
         String feedUrl = feedObj.isNull("url") ? null : feedObj 
           .getString("url"); 
         item.setUrl(feedUrl); 

         feedItems.add(item); 
        } 

        // notify data changes to list adapater 
        listAdapter.notifyDataSetChanged(); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 

     } 

我需要將數組排序在一起以顯示每個數組中最近的項目,然後顯示最近的項目,依此類推。

回答

0

使FeedItem類實現Comparable或Comparator(請參閱this)。然後調用:

Collections.sort(feedItem); 

調用notifyDataSetChanged()