2

I don't know How to sort my ArrayList adapter by date in reverse order

I think my code is fine but my list don't show my array in reversed sort. I tried every way and see many question but I didn't managed to achieve my goal.

Here's my code:

in G class :

public static ArrayList<StructureImage> images = new ArrayList<StructureImage>();

and :

public class StructureImage implements Comparable<StructureImage> {

 public String title;
 public String tozihat;
 public String thumbnail;
 public String url;

 Date dateTime;

 public void setDateTime(Date datetime) {
    this.dateTime = datetime;
 }

 public Date getDateTime() {
    return dateTime;
 }

 @Override
 public int compareTo(StructureImage o) {
    Collections.sort(G.images, Collections.reverseOrder());
    if (getDateTime() == null || o.getDateTime() == null)
        return 0;
    return getDateTime().compareTo(o.getDateTime());
 }
}

and fill method :

public void fill(final ArrayAdapter<StructureImage> adapter, final StructureImage item, final int position) {

 txtTitle.setText(item.title);
 txtTozih.setText(item.tozihat);
 imgPreview.setClickable(true);

 String filename = item.thumbnail.replace("/thumbnail/", "");
 intent.putExtra("filename", filename);
 final File imageFile = new File(G.DIR_FINAL + "/" + filename);
 if ( !imageFile.exists()) {
     imgPreview.setImageBitmap(null);
     DownloadManager.addToDownloadList(item.thumbnail);
 }

 BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 8;
 Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
 imgPreview.setImageBitmap(bitmap);
}

and i use image array :

    private void getImageListFromServer() {
    Date date = new Date(0);
    String result = Webservice.readUrl("http://192.168.1.100/file-server/service.php", null);
    if (result != null) {
        try {
            images.clear();

            JSONArray tasks = new JSONArray(result);
            for (int i = 0; i < tasks.length(); i++) {
                JSONObject object = tasks.getJSONObject(i);
                StructureImage image = new StructureImage();
                image.title = object.getString("title");
                image.tozihat = object.getString("tozihat");
                image.thumbnail = object.getString("thumbnail");
                image.url = object.getString("url");

                DownloadManager.addToDownloadList(image.thumbnail);
                images.add(image);

            }

            adapter.notifyDataSetChanged();
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

2 Answers2

2

You have to call:

Collections.sort(yourListOfComparablesObjects);

to ordering take effect on the list.

The order is not automatically, you must call order whenever you wanna your sorted list.

Your code calls sort on G.images, that is wrong cause this method will never be called if not by a sort on the list.

To change between reverse and normal order you must change from:

   return getDateTime().compareTo(o.getDateTime());

to:

   return o.getDateTime().compareTo(getDateTime());

To your code:

add after:

        JSONArray tasks = new JSONArray(result);
        for (int i = 0; i < tasks.length(); i++) {
            JSONObject object = tasks.getJSONObject(i);
            StructureImage image = new StructureImage();
            image.title = object.getString("title");
            image.tozihat = object.getString("tozihat");
            image.thumbnail = object.getString("thumbnail");
            image.url = object.getString("url");

            DownloadManager.addToDownloadList(image.thumbnail);
            images.add(image);

        }

Do:

           Collections.sort(images);
           adapter.notifyDataSetChanged();

AND REMOVE THE SORT CALL at yours compare method.

Marcos Vasconcelos
  • 17,773
  • 29
  • 104
  • 165
1

..your code is doing some nasty things, are you intending to sort inside of every sort loop? Please, go ahead and remove Collections.sort(G.images, Collections.reverseOrder()); from your compareTo function.

@Override
 public int compareTo(StructureImage o) {
    if (getDateTime() == null || o == null || o.getDateTime() == null)
        return 0;
    return getDateTime().compareTo(o.getDateTime());
 }

..and try again, you may want to call sort Collections.sort line in another place.

Hugo Alonso
  • 6,054
  • 2
  • 26
  • 62
  • ..now you have to sort somewhere, try calling `Collections.sort` after you have already grabbed all your data from JSON, take a look into @Marcos Vasconcelos updated answer. – Hugo Alonso Aug 21 '15 at 22:00