2

i am facing 3 problems with my ArrayList. It reads without any problem files from my sdcard.

Files have this structure:

1.3.2016.csv
2.3.2016.csv
29.2.2016.csv
30.2.2016.csv
4.2.2016.csv

Here is my method to read files from sdcard:

private List<String> getList(File parentDir) {

    ArrayList<String> inFiles = new ArrayList<String>();
    String[] fileNames = parentDir.list();
    for (String fileName : fileNames) {
        if (fileName.toLowerCase().endsWith(".csv")) {
            inFiles.add(removeLastChar(fileName));
        } else {
            File file = new File(File.separator + fileName + ".csv");
            if (file.isDirectory()) {
                inFiles.addAll(getList(((file))));
            }
        }
    }

    itemTexte.addAll(Arrays.asList(fileNames));
    return inFiles;
}

private static String removeLastChar(String str) {
    return str.substring(0,str.length()-4);
}

And here is what i call from onCreate:

List<String> files = getList(new File(Environment.getExternalStorageDirectory() + File.separator + "Test" + File.separator + "Files"));
  1. First problem. I don't want to see the ".csv" in my recycler view. So i try to remove 4 chars from it (see removeLastChar method). But it stays at ".csv" at the end no matter what i do.

  2. I want to order the recycler view, by the name of the files. For now it does not order it by the name. I tried the method Comparator, but i didn't understood how, i should parse my elements there.

  3. Even if my files don't have the ending ".csv", they are still shown in the recycler view. Looks like it catch all files that are in the folder.

UPDATE I can sort my list with this(thanks guys):

    Collections.sort(inFiles);

But it sorts kind of wrong... Example:

1.3.2016
10.2.2016
11.4.2016
2.3.2016
3.3.2016

But it should be something like this:

10.2.2016
1.3.2016
2.3.2016
3.3.2016
11.4.2016

Is there any easy fix? Or should i implement writing "0" to the files when user create it?

UPDATE 2

So i added this

Collections.sort(inFiles, new Comparator<String>() {

        @Override
        public int compare(String s1, String s2) {
            String[] s1Values = s1.split(".");
            String[] s2Values = s1.split(".");
            //define your comparison, arrays will have each value between '.', e.g. {"1", "3", "2016", "csv" } for "1.3.2016.csv"
            int compare_result = s1.compareTo(s2);
            return compare_result; //return -1 if s1 < s2, 0 if equal, 1 if s1 > s2
        }
    });

My list looks like this

01.04.2016
02.04.2016
15.04.2016
16.03.2016

But the 16.03.2016 should be first... Thanks for help in advance!

Wladislaw
  • 1,200
  • 2
  • 12
  • 23

3 Answers3

2

1. Your remove file extension method should work. I think your issue is that you don't add your new list to the itemTexte, you add the original list of files.

Change

itemTexte.addAll(Arrays.asList(fileNames));

to

itemTexte.addAll(inFiles );

2.

Collections.sort(inFiles);

3. See the fix for #1

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • Please see the updated question. It worked almost how it should. Only sort function is wrong. Thanks anyway! – Wladislaw Apr 01 '16 at 14:32
  • Oh, I remember that! It's not the first time... http://stackoverflow.com/questions/14451976/how-to-sort-date-which-is-in-string-format-in-java, it helps to me a lot! – M. Mariscal Apr 01 '16 at 14:41
  • 2
    @Wladislaw You will want to use a comparator. i will update my answer with an example. – James Wierzba Apr 01 '16 at 14:41
  • @Wladislaw I've edited my answer, but left some of the implementation of the comparator out, as it will be up to you to decide on how to order them. – James Wierzba Apr 01 '16 at 15:10
  • @JamesWierzba can you check what is wrong with the code from you? I mean when i implement it, it looks like it does not even sort it...look the updated question. – Wladislaw Apr 01 '16 at 15:47
  • @Wladislaw you return 1 every time, which means you are saying, `s1` is always greater than `s2`. you need to find out which string (`s1` or `s2`) is greater, and return the appropriate value. – James Wierzba Apr 01 '16 at 15:49
  • @JamesWierzba i am sorry... can you give more hint, how i can get second parameter (month) to sort too...with more priority? Look the updated question... – Wladislaw Apr 01 '16 at 18:39
  • @Wladislaw Ok, I've implemented it for you, please see my new answer – James Wierzba Apr 01 '16 at 19:01
1

Here is and more complete example of a comparator for you to use to sort your special file names.

I've added checks in-case the file is not in the form "*.*.*"

    Collections.sort(inFiles, new Comparator<String>(){
        @Override
        public int compare(String s1, String s2)
        {
            String[] s1Values = s1.split("\\.");
            String[] s2Values = s2.split("\\.");
            //compare the year
            if(s1Values.length >= 3 && s2Values.length >= 3)
            {
                int compare = s1Values[2].compareTo(s2Values[2]);
                if(compare != 0) return compare;
            }
            //compare the month
            if(s1Values.length >= 2 && s2Values.length >= 2)
            {
                int compare = s1Values[1].compareTo(s2Values[1]);
                if(compare != 0) return compare;
            }
            //compare the day
            if(s1Values.length >= 1 && s2Values.length >= 1)
            {
                int compare = s1Values[0].compareTo(s2Values[0]);
                if(compare != 0) return compare;
            }
            return 0; 
        }
    });
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
0

1. You should use split to catch just fileName, I will show you an example right down

2. To sort alphabetically do java.util.Collections.sort(listOfCSV)

String string = "fileName.csv";
String[] parts = string.split(".");
String firstPart = parts[0]; // fileName
String secondPart = parts[1]; // csv

Hope it helps!

M. Mariscal
  • 1,226
  • 3
  • 17
  • 46
  • Please see the updated question. It worked almost how it should. Only sort function is wrong. Thanks anyway! – Wladislaw Apr 01 '16 at 14:33
  • Oh, I remember that! It's not the first time... http://stackoverflow.com/questions/14451976/how-to-sort-date-which-is-in-string-format-in-java, it helps to me a lot! It's so simple, using Comparator – M. Mariscal Apr 01 '16 at 14:42