Hello developers, if you want to sort Document Files list in Android Studio java. In this Article we have provided four different types of sorts function where you can Sort Document Files by Its Name, Last Modified Date, Size and Is File/Directory. For example- After created a Document File from Tree Uri, we can get a Document File list inside Tree Uri Document File. Now if you want to sort that Document Files list then you are in the right place. If you want more documentation related to Document File or Intent.ACTION_OPEN_DOCUMENT_TREE then, do comment below this Article and we will try to make more Article related to this.
Sorting a document file in Android Studio can be useful for organizing large amounts of Document file list, making it easier to find what you're looking for. The process of sorting a document file involves reordering the contents of the file based on a specific criteria, such as alphabetical order or date modified. DocumentFile is a class in Android Studio that provides a simplified way to access and manipulate files and directories on the device. It is particularly useful when working with files in external storage, such as an SD card. Sorting a DocumentFile is similar to sorting any other type of file in Android Studio, with a few differences in the implementation. Here are the steps to sort a DocumentFile in Android Studio:
Sort Document Files list by Name
public static void sortDocumentListByName(final DocumentFile[] listMap, final boolean ascending) {
// you can sort document files by its name
try {
Arrays.sort(listMap, new Comparator<DocumentFile>() {
@Override
public int compare(DocumentFile o1, DocumentFile o2) {
if (ascending) {
return (o1.getName().compareTo(o2.getName()));
} else {
return (o2.getName().compareTo(o1.getName()));
}
}
});
} catch (Exception e) {
}
}
Sort Document Files list by Last Modefied Date
public static void sortDocumentListByModifiedDate(final DocumentFile[] listMap, final boolean ascending) {
// you can sort document files by last modified date
try {
Arrays.sort(listMap, new Comparator<DocumentFile>() {
@Override
public int compare(DocumentFile o1, DocumentFile o2) {
if (ascending) {
return Long.compare(o1.lastModified(), o2.lastModified());
} else {
return Long.compare(o2.lastModified(), o1.lastModified());
}
}
});
} catch (Exception e) {
}
}
Sort Document Files list by its Size/length
public static void sortDocumentListBySize(final DocumentFile[] listMap, final boolean ascending) {
// you can sort document files by its size/length
//this function may not work for directories. Because directory length sometimes return wrong value
try {
Arrays.sort(listMap, new Comparator<DocumentFile>() {
@Override
public int compare(DocumentFile o1, DocumentFile o2) {
if (ascending) {
return Long.compare(o1.length(), o2.length());
} else {
return Long.compare(o2.length(), o1.length());
}
}
});
} catch (Exception e) {
}
}
Sort Document Files list by File and Directory
public static void sortDocumentListByIsFile(final DocumentFile[] listMap, final boolean ascending) {
// you can list all files on the top/bottom and directories on the bottom/top by this sort
try {
Arrays.sort(listMap, new Comparator<DocumentFile>() {
@Override
public int compare(DocumentFile o1, DocumentFile o2) {
if (ascending) {
return Boolean.compare(o1.isFile(), o2.isFile());
} else {
return Boolean.compare(o2.isFile(), o1.isFile());
}
}
});
} catch (Exception e) {
}
}
Integration Example
Uri treeUri = Uri.parse("tasting"); // paste your tree uri
DocumentFile documentFile = DocumentFile.fromTreeUri(MainActivity.this, treeUri);
DocumentFile[] documentFiles = documentFile.listFiles();
sortDocumentListByName(documentFiles, false);
sortDocumentListBySize(documentFiles, false);
sortDocumentListByModifiedDate(documentFiles, false);
sortDocumentListByIsFile(documentFiles, false);
You can sort this list in ascending or descending order by providing ascending value true/false. We also used 'try catch exception', if error found while sorting Document File it will through an Exception. For example, a document file inside document list then it will through file not found exception or if documentfile.canRead() return false then it will through Security Exception.
Overall, sorting a DocumentFile in Android Studio is a similar process to sorting any other type of file, with the added benefit of simplified access and manipulation of files and directories on the device.Sorting a DocumentFile in Android Studio is a useful way to organize files on the device's external storage. With the DocumentFile class and its methods, sorting files by different criteria is made easy and secure.
Thank you for visiting this Article. If you face any error while integration, then you can contact us anytime. We will try to help you.
.png)
0 Comments