Sort directory/file in alphabetical order ignore case

Posted: May 15, 2013 in Android Posts, Android Tutorials
Tags: , , , , , , ,

Last exercise of “File Explorer” list file/directory without order. We can implement Comparator<? super File> to sort the directory/file in alphabetical order. We can also convert the file name to lower case before comparing, such that the array will be sorted in alphabetical order ignore case.

Sort directory/file in alphabetical order ignore case

Modify MainActivity.java from last exercise “File Explorer”.

package com.example.androidexplorer;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import android.os.Bundle;
import android.os.Environment;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

 private List<String> item = null;
 private List<String> path = null;
 private String root;
 private TextView myPath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myPath = (TextView)findViewById(R.id.path);

        root = Environment.getExternalStorageDirectory().getPath();

        getDir(root);
    }

    private void getDir(String dirPath)
    {
     myPath.setText("Location: " + dirPath);
     item = new ArrayList<String>();
     path = new ArrayList<String>();
     File f = new File(dirPath);
     File[] files = f.listFiles();

     if(!dirPath.equals(root))
     {
      item.add(root);
      path.add(root);
      item.add("../");
      path.add(f.getParent()); 
     }

     Arrays.sort(files, filecomparator);

     for(int i=0; i < files.length; i++)
     {
      File file = files[i];

      if(!file.isHidden() && file.canRead()){
       path.add(file.getPath());
          if(file.isDirectory()){
           item.add(file.getName() + "/");
          }else{
           item.add(file.getName());
          }
      } 
     }

     ArrayAdapter<String> fileList =
       new ArrayAdapter<String>(this, R.layout.row, item);
     setListAdapter(fileList); 
    }

    Comparator<? super File> filecomparator = new Comparator<File>(){

  public int compare(File file1, File file2) {

   if(file1.isDirectory()){
    if (file2.isDirectory()){
     return String.valueOf(file1.getName().toLowerCase()).compareTo(file2.getName().toLowerCase());
    }else{
     return -1;
    }
   }else {
    if (file2.isDirectory()){
     return 1;
    }else{
     return String.valueOf(file1.getName().toLowerCase()).compareTo(file2.getName().toLowerCase());
    }
   }

  }  
 };

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub
  File file = new File(path.get(position));

  if (file.isDirectory())
  {
   if(file.canRead()){
    getDir(path.get(position));
   }else{
    new AlertDialog.Builder(this)
     .setIcon(R.drawable.ic_launcher)
     .setTitle("[" + file.getName() + "] folder can't be read!")
     .setPositiveButton("OK", null).show(); 
   } 
  }else {
   new AlertDialog.Builder(this)
     .setIcon(R.drawable.ic_launcher)
     .setTitle("[" + file.getName() + "]")
     .setPositiveButton("OK", null).show();

    }
 }

}

Download the files.

Enhanced by Zemanta
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s