Ronit Jain
Ronit Jain

Reputation: 45

Android : Sort Array list of Strings containing paths of all the .jpg files on the mobile

Can anyone help me about how to sort array list of string containing paths. i used Collection.sort(my_list); but it didn't worked fine , it sorts them in order according to the folder names like suppose i have files :

mnt/sdcard/A.jpg
mnt/sdcard/folder1/B.jpg
mnt/sdcard/C.jpg
mnt/sdcard/folder2/D.jpg

after Collections.sort(my_list) :

mnt/sdcard/A.jpg
mnt/sdcard/c.jpg
mnt/sdcard/folder1/b.jpg
mnt/sdcard/folder2/d.jpg

i want results like :

 mnt/sdcard/A.jpg
    mnt/sdcard/folder1/B.jpg
    mnt/sdcard/C.jpg
    mnt/sdcard/folder2/D.jpg

Upvotes: 2

Views: 147

Answers (3)

Blackbelt
Blackbelt

Reputation: 157437

String name = new File("mnt/sdcard/c.jpg").getName();

returns c.jpg

Use it for each String in your Collection, in your comparator to sort the List

Upvotes: 1

mattfred
mattfred

Reputation: 2749

One way to do it is to get the substring of each string after the last "/". Then you can sort by that value.

String sub = myString.substring(string.lastIndexOf('/') + 1);

Upvotes: 0

Renjith
Renjith

Reputation: 3274

Implement a custom Comparator and pass that comparator object to the sort function. While implementing comparator,split for file path and get that last part alone. Then return the result comparing these last parts.

More details here

Upvotes: 0

Related Questions