user2448122
user2448122

Reputation: 335

Sort a string array that contains relative filenames

Consider the following string array, which is a list of file names with relative path:

String[] myArray = { "src\\kop\\cKOP_C.o"
                   , "src\\io\\cIO_S.o"
                   , "src\\io\\com\\cGA_M.o"
                   , "src\\io\\cADC.o"
                   , "src\\io\\cIO_H.o"
                   , "src\\io\\com\\com_m.o"
                   , "src\\io\\tab_s.o"
                   , "src\\kop\\cKOP" };

The directory layout is:

src\io\cADC.o
src\io\cIO_H.o
src\io\cIO_S.o
src\io\tab_s.o
src\io\com\cGA_M.o
src\io\com\com_m.o
src\kop\cKOP_A.o
src\kop\cKOP_B.o

I want to sort this array in java to have a result like this:

src\\io\\cADC.o
src\\io\\cIO_H.o
src\\io\\cIO_S.o
src\\io\\tab_s.o
src\\io\\com\\cGA_M.o
src\\io\\com\\com_m.o
src\\kop\\cKOP_A.o
src\\kop\\cKOP_B.o

Currently I use Array.Sort(myArray), but the result is like this:

src\\io\\cADC.o
src\\io\\cIO_H.o
src\\io\\cIO_S.o
src\\io\\com\\cGA_M.o
src\\io\\com\\com_m.o
src\\io\\tab_s.o
src\\kop\\cKOP_A.o
src\\kop\\cKOP_B.o

This is not what I want, because I want to keep the directory information, so list first all files within a directory, and if there is a sub-directory within, list the files in the sub-dir afterwards.

I know I have to implement a Comparator() for the array to be sorted as I want, but I cannot figure out the logic behind the compare.

Upvotes: 2

Views: 152

Answers (3)

Joe
Joe

Reputation: 6827

My solution:

    Arrays.sort(myArray,new Comparator<String> ()
    {
        public int compare(String a,String b)
        {
            if(a.equals(b)) // same path/filename
                return 0;
            int aBSCount = a.replaceAll("[^\\\\]","").length();
            int bBSCount = b.replaceAll("[^\\\\]","").length();

            if(aBSCount==bBSCount) // same directory depth
                return a.compareTo(b);

            String[] aSplit = a.split("\\\\");
            String[] bSplit = b.split("\\\\");

            int shortest = Math.min(aSplit.length, bSplit.length);

            for(int x = 0;x < shortest;x++)
            {
                if(x==shortest-1)
                    return aBSCount < bBSCount ? -1 : 1;

                if(!(aSplit[x].equals(bSplit[x])))
                    return aSplit[x].compareTo(bSplit[x]);
            }

            return aBSCount < bBSCount ? -1 : 1;
        }
    });

Upvotes: 2

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

Try this

    Arrays.sort(a, new Comparator<String>() {
        public int compare(String o1, String o2) {
            File f1 = new File(o1);
            File f2 = new File(o2);
            int c1 = f1.getParent().compareTo(f2.getParent());
            if (c1 > 0) {
                return 1;
            }
            if (c1 < 0) {
                return -1;
            }
            return f1.getName().compareTo(f2.getName());
        }

Upvotes: 0

hamid
hamid

Reputation: 2079

You just need to implement sort:

 Arrays.sort(myArray, new Comparator<String>() {
        @Override
        public int compare(String arg0, String arg1) {
            arg0 = arg0.toLowerCase();
            arg1 = arg1.toLowerCase();
            return arg0.compareTo(arg1);
        }
    });

Upvotes: 0

Related Questions