-1

I have an arraylist of Files. I want to sort all the files according to the file name.

when I apply Collections.sort(files); to the arraylist following result was given.

(files = arraylist of files)

/data/data/com.threepi.icheops/files/xxx/image_1_0.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_1.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_10.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_11.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_2.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_3.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_4.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_5.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_6.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_7.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_8.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_9.jpg

I want these files to be like this.

/data/data/com.threepi.icheops/files/xxx/image_1_0.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_1.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_2.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_3.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_4.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_5.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_6.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_7.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_8.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_9.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_10.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_11.jpg

any ideas how to do this.

2
  • implement Comparator interface and implement/use compare() method.. Put your sorting logic inside the compare() method.. this is happening because the names are being sorted alphabetically... so 10 comes earlies than 2... Commented Dec 27, 2013 at 11:53
  • it seams like an alphabetic sort, try this stackoverflow.com/questions/11176227/… and stackoverflow.com/questions/14475556/… Commented Dec 27, 2013 at 11:54

3 Answers 3

1

here's an idea.. implement Comparator interface and implement/use compare() method. In your compare method split the strings based on "_" ... In arr[2] you will get 0,1,2... 10,11.. now parse these as integers using int i= Integer.parseInt(arr[2]).. and sort your Strings based on those integer values...

Sign up to request clarification or add additional context in comments.

Comments

0

you can use CustomComparator

public class CustomComparator implements Comparator<MyObject> {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        return o1.getStartDate().compareTo(o2.getStartDate());
    }
}

and then after use

Collections.sort(arrayList, new CustomComparator());

Comments

-1

Yo should try to rename the files putting the next names because when you sort a String it's different to a number:

/data/data/com.threepi.icheops/files/xxx/image_1_00.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_01.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_02.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_03.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_04.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_05.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_06.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_07.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_08.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_09.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_10.jpg, /data/data/com.threepi.icheops/files/xxx/image_1_11.jpg

1 Comment

If He is doing a alphabetical sort it's an explanation what is getting this result. Obviously there are another options to solve this problem.

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.