How to sort files using datetimestamp

后端 未结 3 1562
生来不讨喜
生来不讨喜 2020-12-20 09:16

I am capturing images, then storing into SD Card and showing in a List, but here i need a small change, still

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 09:56

    You can use the Collections.sort method in your list adapter to sort the values according to the image file name like:

    Collections.sort(ImageList, new Comparator() {
      int compare(String obj1, String obj2) {
            return obj1.compareTo(obj2);
      }
    });
    

    compareTo method and also compareToIgnoreCase method, use wichever you think is appropriate, and also, you can experiment with obj1 and obj2, that is, you could swap the condition to:

     return obj2.compareTo(obj1);
    

    That way your list will be sorted. Hope that helps!

    EDIT:

    Since you know that the format is _ and then -.jpg, what you can do is in the comparator split the value from - like:

    Collections.sort(ImageList, new Comparator() {
      int compare(String obj1, String obj2) {
            String[] obj1Arr = obj1.split(-);
            String[] obj2Arr = obj2.split(-);
    
            obj1Arr = obj1Arr[1].split("."); // to just get the counter value
            obj2Arr = obj2Arr[1].split(".");
    
            return obj1Arr[0].compareTo(obj2Arr[0]);
      }
    });
    

提交回复
热议问题