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
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.