title: Sort ArrayList<HashMap<String,String>> based on datetime
body: <p>I'm new on android programming, and still learning about its concept, I'm currently building an app that will get some data from online database and stored it in ArrayList> Type, then I will show the data,</p>
  
  <p>I've successfully get the data from database and successfully showed it on ListView as well,, and now I want to sort the data based on its date (there is one date value stored in the hashmap),</p>
  
  <p>I've read how to do it in these question :</p>
  
  <p><a href="https://stackoverflow.com/questions/22812349/how-to-sort-data-of-arraylist-of-hashmapstring-string-on-the-basis-of-date">How to sort data of ArrayList of hashmap on The Basis of Date</a></p>
  
  <p>I don't really get the concept and still have no idea how it could be worked with my current code. Hope you could help me with my code, </p>
  
  <p>This is my code :</p>
  
  <pre><code>public class Notification extends Activity {
  
  userSessionManager session;
  String Username, clickedId, clickedTitle, toastMessage;
  String urlUpdateGroupConfirmation, urlGetNotif;
  JSONParser jsonParser;
  ProgressDialog pd;
  JSONArray jsonArray = null;
  private ArrayList&lt;HashMap&lt;String, String&gt;&gt; whatsNew;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.notification);
  
      getActionBar().setDisplayShowHomeEnabled(false);
      sessionAndDeclaration();
      new AttemptParseNotification().execute();
  }
  
  private void sessionAndDeclaration() {
      // TODO Auto-generated method stub
      session = new userSessionManager(getApplicationContext());
      HashMap&lt;String, String&gt; user = session.getUserDetails();
      Username = user.get(userSessionManager.KEY_USERNAME);
      myIP ip = new myIP();
      String publicIp = ip.getIp();
      String thisPhp = "viewMyNotification.php";
      String updateGConf = "doUpdateGroupDetail.php";
  
      urlGetNotif = publicIp + thisPhp;
      urlUpdateGroupConfirmation = publicIp + updateGConf;
      jsonParser = new JSONParser();
      whatsNew = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
  }
  
  class myMapComparator implements Comparator&lt;Map&lt;String, String&gt;&gt; {
  
      @Override
      public int compare(Map&lt;String, String&gt; lhs, Map&lt;String, String&gt; rhs) {
          // TODO Auto-generated method stub
          DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  
          try {
              return df.parse(lhs.get("date")).compareTo(
                      df.parse(rhs.get("date")));
  
          } catch (ParseException e) {
              throw new IllegalArgumentException(e);
          }
      }
  
  }
  
  class AttemptParseNotification extends AsyncTask&lt;Void, Void, Boolean&gt; {
  
      @Override
      protected void onPreExecute() {
          // TODO Auto-generated method stub
          super.onPreExecute();
          pd = new ProgressDialog(Notification.this);
          pd.setIndeterminate(false);
          pd.setCancelable(true);
          pd.setMessage("Loading...");
          pd.show();
      }
  
      @Override
      protected Boolean doInBackground(Void... arg0) {
          // TODO Auto-generated method stub
          int success = 0;
          try {
              List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
              params.add(new BasicNameValuePair("Username", Username));
              Log.d("Request!", "Passing Username to server");
              JSONObject json = jsonParser.makeHttpRequest(urlGetNotif,
                      "POST", params);
              success = json.getInt("success");
              if (success == 1) {
                  Log.d("Response", "Getting todays");
                  jsonArray = json.getJSONArray("array");
                  try {
                      for (int i = 0; i &lt; jsonArray.length(); i++) {
                          JSONObject c = jsonArray.getJSONObject(i);
                          String newId = c.getString("id");
                          String newType = c.getString("type");
                          String newTitle = c.getString("title");
                          String newDisplayed = newTitle + "(" + newType
                                  + ")";
                          String newDate = c.getString("date");
                          HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                          map.put("id", newId);
                          map.put("title", newTitle);
                          map.put("date", newDate);
                          map.put("type", newType);
                          map.put("displayed", newDisplayed);
                          whatsNew.add(map);
                          Collections.sort(whatsNew, new myMapComparator());
  
                      }
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
  
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(Boolean result) {
          // TODO Auto-generated method stub
          super.onPostExecute(result);
          pd.dismiss();
          if (whatsNew.size() &gt; 0) {
  
              viewNews();
          } else {
              Toast.makeText(getBaseContext(), "No new notification",
                      Toast.LENGTH_LONG).show();
          }
      }
  
  }
  
  public void viewNews() {
      // TODO Auto-generated method stub
      ListView lv = (ListView) findViewById(R.id.lv_notif);
      ListAdapter adapter = new SimpleAdapter(this, whatsNew,
              R.layout.notificationlist_item, new String[] { "title", "type",
                      "date" }, new int[] { R.id.title_notif,
                      R.id.type_notif, R.id.date_notif });
      lv.setAdapter(adapter);
  
  }
  }
  </code></pre>