0

I want to sort this

LG Electronics  1.jpg   
Apple   2.JPG   
Apple   3.JPG

As

Apple   2.JPG   
Apple   3.JPG
LG Electronics  1.jpg   

Here is my code //rows is 2d

ArrayList<String[]> rows = new ArrayList<>();
for(int i=0;i<images.length;i++){
    com.drew.metadata.Metadata metadata = ImageMetadataReader.readMetadata(images[i]);
    for (com.drew.metadata.Directory directory : metadata.getDirectories()) {
        for (Tag tag : directory.getTags()) {
            //System.out.println(tag.toString());
            if(tag.toString().contains("[Exif IFD0] Make")){
                rows.add(new String[]{tag.getDescription(),images[i].toString()});
            }
        }
    }
}

I have implemented Collections.sort(rows); but nothing works for me. I even tried

Collections.sort(rows, new Comparator<ArrayList<String>>() {
    @Override
    public int compare(ArrayList<String> o1, ArrayList<String> o2) {
    return o1.get(0).compareTo(o2.get(0));
    }
    });

But it also doesn't works for me. I got this error that no suitable method found for sort(arraylist

3
  • rows is a List<String[]>. So its elements are String[]. So, to compare them, you need a Comparator<String[]>. Commented Nov 14, 2018 at 22:53
  • Can you help me doing that I'm really stuck implementing everything. Seems nothing works for me.. Commented Nov 14, 2018 at 22:59
  • Check this link stackoverflow.com/questions/21664875/… it mentions an item from Josh Bloch's book Effective Java... where he clearly mentions never mix Generics with Array. That's first thing... In general Josh suggests that try to keep the things simple in your programming and then solutions will just come simple as well... Commented Nov 14, 2018 at 23:38

1 Answer 1

2

I would advise against using a model such as ArrayList<String[]> rows in your case because it really doesn't tell much about what the list is holding -- and it makes implementing the comparison clunky.

Instead, you could model the metadata as a Java class:

public class Metadata {

  private final String description;
  private final String imageName;

  public Metadata(String description, String imageName) {
    this.description = description;
    this.imageName = imageName;
  }
  public String getDescription() {return description;}
  public String getImageName() {return imageName;}

  @Override
  public String toString() {
    return description + " " + imageName;
  }
}

Now, you can have a List<Metadata> rows = new ArrayList<>(); which you populate inside the loop only changing this part:

rows.add(new String[]{tag.getDescription(),images[i].toString()});

into this

rows.add(new Metadata(tag.getDescription(), images[i].toString());

And finally, you can sort with a proper Comparator using

Collections.sort(rows, Comparator
                        .comparing(Metadata::getDescription)
                        .thenComparing(Metadata::getImageName));
Sign up to request clarification or add additional context in comments.

4 Comments

Glad to help. If you think this answers your question, you can also choose to accept the answer.
Now what if I want to get the original values after sorting like: Apple 2.JPG Apple 3.JPG LG Electronics 1.jpg Now when i print rows after sorting all i get is output like` sorting.Data@387f0054` How can I can get specific values like imageName
I modified the Metadata class to include a toString() implementation which should take care of the problem (and you can change as you wish). I think there is a checkbox you can tick in the UI somewhere for accepting the answer.
done brother that was really helpful... I was stuck here but i think grateful to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.