Android programming to achieve the method of sorting files in folders

  • 2021-01-14 06:38:13
  • OfStack

This article describes an example of Android programming to achieve the method of sorting files in folders. To share with you for your reference, as follows:


private int mFileSize = 0;
private List<String> mPathString = new ArrayList<String>();
private boolean sortFolder(String path) {
    if (path == null || StringUtil.isEmpty(path))
      return false;
    File[] fileList = null;
    File file = new File(path);
    if (file.exists() == false){
      file.mkdir();
    }
    if (!file.exists() || (file.isDirectory() && (file.listFiles().length == 0))) {
      return true;
    } else {
      fileList = file.listFiles();
      mFileSize = file.listFiles().length;
  mPathString.clear();
      if(mFileSize > 0) {
        for(int i = 0;i < mFileSize;i++) {
          mPathString.add(fileList[i].getAbsolutePath());
        }
        Collections.sort(mPathString);
      }
      return false;
    }
}

PS: Parameter path is the path to the folder
For descending sort

Collections.reverse(mPathString);

For more information on Android file operation, readers interested in Android file operation can check out the special topic of this site: Android file operation tips summary

I hope this article is helpful to Android program design.


Related articles: