title: How to sort file\directory tree in lexicographic order (case-insensitive) on Linux system
body: <p>I have a class that do a tree from directory, subdirectory and files. And need to satisfy two conditions. Sort the contents in following way:
  -Directories should go first.
  -Directories and files are sorted in lexicographic order (case-insensitive).
  I work on windows system and code below work fine, all is sorted how I want. Directories go first and are sorted in lexicographic order. But I read that on windows system is automatically sorted in lexicographic order. But this code doesn't work on Linux, the line <code>children.sort(Comparator.comparing(file -&gt; file.isDirectory() ? -1 : 1));</code> do not work and files\directory aren't sorted in lexicographic order (case-insensitive). How to solve problem with sort conditions on linux system? How can change sort conditions in code?</p>
  <pre><code>import static java.util.Comparator.comparing;
  
  public class TreeNode&lt;T&gt; implements Iterable&lt;TreeNode&lt;T&gt;&gt; {
  
      public T data;
      public TreeNode&lt;T&gt; parent;
      public List&lt;TreeNode&lt;T&gt;&gt; children;
  
      public boolean isRoot() {
          return parent == null;
      }
  
      private List&lt;TreeNode&lt;T&gt;&gt; elementsIndex;
  
      public TreeNode(T data) {
          this.data = data;
          this.children = new LinkedList&lt;TreeNode&lt;T&gt;&gt;();
          this.elementsIndex = new LinkedList&lt;TreeNode&lt;T&gt;&gt;();
          this.elementsIndex.add(this);
      }
  
      public TreeNode&lt;T&gt; addChild(T child) {
          TreeNode&lt;T&gt; childNode = new TreeNode&lt;T&gt;(child);
          childNode.parent = this;
          this.children.add(childNode);
          this.registerChildForSearch(childNode);
          return childNode;
  
      }
  
      private void registerChildForSearch(TreeNode&lt;T&gt; node) {
          elementsIndex.add(node);
          if (parent != null)
              parent.registerChildForSearch(node);
      }
  
      @Override
      public String toString() {
          return data != null ? data.toString() : &quot;[data null]&quot;;
      }
  
      @Override
      public Iterator&lt;TreeNode&lt;T&gt;&gt; iterator() {
          TreeNode&lt;T&gt; iter = new TreeNode&lt;T&gt;((T) this);
          return (Iterator&lt;TreeNode&lt;T&gt;&gt;) iter;
      }
  
      public static TreeNode&lt;File&gt; createDirTree(File folder) {
          if (!folder.isDirectory()) {
              throw new IllegalArgumentException(&quot;folder is not a Directory&quot;);
          }
  
         List&lt;File&gt; children = Arrays.asList(folder.listFiles());
  children.sort(Comparator.comparing(file -&gt; file.isDirectory() ? -1 : 1));
  
          TreeNode&lt;File&gt; DirRoot = new TreeNode&lt;File&gt;(folder);
  for (File file : children) {
              if (file.isDirectory()) {
                  appendDirTree(file, DirRoot);
              } else {
                  appendFile(file, DirRoot);
              }
          }
          return DirRoot;
      }
  
      public static void appendDirTree(File folder, TreeNode&lt;File&gt; dirRoot) {
          dirRoot.addChild(folder);
  
          List&lt;File&gt; children = Arrays.asList(folder.listFiles());
          children.sort(comparing(file -&gt; file.isDirectory() ? -1 : 1));
  for (File file : children) {
              if (file.isDirectory()) {
                  appendDirTree(file, dirRoot.children.get(dirRoot.children.size() - 1));
              } else {
                  appendFile(file, dirRoot.children.get(dirRoot.children.size() - 1));
              }
          }
      }
  
      public static void appendFile(File file, TreeNode&lt;File&gt; filenode) {
          filenode.addChild(file);
  
      }
  
  
      public static String renderDirectoryTree(TreeNode&lt;File&gt; tree) {
          List&lt;StringBuilder&gt; lines = renderDirectoryTreeLines(tree);
          String newline = &quot;\n&quot;;
          StringBuilder sb = new StringBuilder(lines.size() * 20);
          for (StringBuilder line : lines) {
              sb.append(line);
              sb.append(newline);
          }
          //System.out.println(sb);
          return sb.toString();
      }
  
      public static List&lt;StringBuilder&gt; renderDirectoryTreeLines(TreeNode&lt;File&gt; tree) {
  
          List&lt;StringBuilder&gt; result = new LinkedList&lt;&gt;();
          result.add(new StringBuilder().append(tree.data.getName() + &quot; &quot; + calculateFileSize(tree) + &quot; bytes&quot;));
          Iterator&lt;TreeNode&lt;File&gt;&gt; iterator = tree.children.iterator();
          while (iterator.hasNext()) {
              List&lt;StringBuilder&gt; subtree = renderDirectoryTreeLines(iterator.next());
              if (iterator.hasNext()) {
                  addSubtree(result, subtree);
              } else {
                  addLastSubtree(result, subtree);
              }
          }
  
          return result;
      }
  
      private static void addSubtree(List&lt;StringBuilder&gt; result, List&lt;StringBuilder&gt; subtree) {
          Iterator&lt;StringBuilder&gt; iterator = subtree.iterator();
          result.add(iterator.next().insert(0, &quot;├─ &quot;));
          while (iterator.hasNext()) {
              result.add(iterator.next().insert(0, &quot;│  &quot;));
          }
      }
  
      private static void addLastSubtree(List&lt;StringBuilder&gt; result, List&lt;StringBuilder&gt; subtree) {
          Iterator&lt;StringBuilder&gt; iterator = subtree.iterator();
          result.add(iterator.next().insert(0, &quot;└─ &quot;));
          while (iterator.hasNext()) {
              result.add(iterator.next().insert(0, &quot;   &quot;));
          }
      }
  
  public static long calculateFileSize(TreeNode&lt;File&gt; tree) {
      long fileSize = 0;
      if (tree.data.isDirectory()) {
          List&lt;TreeNode&lt;File&gt;&gt; children = tree.children;
  
  
          for (TreeNode&lt;File&gt; child : children) {
              fileSize += calculateFileSize(child);
          }
  
      } else {
          fileSize = tree.data.length();
      }
      return fileSize;
  }
  </code></pre>
  <p>}</p>