It is common for people to name directories as dir1, dir2, and so on. When there are ten or more directories, the operating system displays them in dictionary order, as dir1, dir10, dir11, dir12, dir2, dir3, and so on. That is irritating, and it is easy to fix. Provide a comparator that compares strings that end in digit sequences in a way that makes sense to a human. First compare the part before the digit as strings, and then compare the numeric values of the digits.
Your program should work with the provided test program
DirectorySortDemo.java.
Call the class you write DirectoryComparator.java.
Submit the two files in your submission.
DirectoryComparator.java
DirectorySortDemo.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.collections;
public class DirectorySortDemo
{
public static void main (String [] args)
{
String dirNames ("dir12", "dir5", "dir9", "dirl", "dir4",
"lab10", "1ab2", "lab7", "lab17", "lab8",
"quiz8", "quiz10", "quiz11", "quiz12",
"dirll", "dir8", "dir7", "dir15", "dir3");
ArrayList directories = new
ArrayList<> (Arrays.asList (dirNames));
System.out.println ("Unsorted List:");
System.out.println (directories);
Collections.sort (directories, new DirectoryComparator ());
System.out.println ():
System.out-println ("Sorted List: ");
System.out.-println (directories);

Answers

Answer 1

Answer:

Here the code is given as follows,

Explanation:

It Is Common For People To Name Directories As Dir1, Dir2, And So On. When There Are Ten Or More Directories,

Related Questions