How to list files in alphabetical order?

August 23, 2020 No comments Java IO FIles List Sort

1. Introduction

In this short article, we are going to present how to get a list of files in alphabetical order in Java.

2. Sort file list using the sorted method from JDK 8

To print files in alphabetical order first we need to list all the files from specified directory. In the following example, we presented a method provided in JDK 8 Files.list(...).


import java.nio.file.Files;
import java.nio.file.Paths;

public class ListFilesInAlphabeticalOrder {

    public static void main(String[] args) throws IOException {


This approach works, because Path is a Comparable class, which by default sorts pathnames lexicographically

3. Conclusion

In this practical tutorial, we presented how to sort a list of files in Java alphabetically. Always the first step is to get a list of files in any format (Array, List, Stream). Then we sort that collection using known Java mechanism.

{{ message }}

{{ 'Comments are closed.' | trans }}