<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.io.*;
import java.util.*;

/**
 * An application that sorts a list of directories
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class DirectorySorter
{
    /**
     * The entry point
     *
     * The command line arguments should contain any switches
     * as well as the list of directories to sort
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args)
    {
	File                    tempFile;
	File[]                  files;
	Comparator              comparatorToUse;
	int                     i, directoryIndex, numberOfDirectories;

	// Count the number of command line arguments
	// that correspond to existing directories
	numberOfDirectories = 0;
	for (i=0; i &lt; args.length; i++)
	{
	    tempFile = new File(args[i]);
	    if (tempFile.exists() &amp;&amp; tempFile.isDirectory())
	    {
		numberOfDirectories++;
	    }
	}

	// Create an array that contains the 
	// existing directories
	files = new File[numberOfDirectories];
	directoryIndex = 0;
	for (i=0; i &lt; args.length; i++)
	{
	    tempFile = new File(args[i]);
	    if (tempFile.exists() &amp;&amp; tempFile.isDirectory())
	    {
		files[directoryIndex] = tempFile;
		directoryIndex++;
	    }
	}


	// Construct the appropriate Comparator
	comparatorToUse = new DirectoryNameComparator();

	if (containsSwitch(args, "-s"))
	{
	    comparatorToUse = new DirectoryDateComparator();
	}


	// Sort the array
	Arrays.sort(files, comparatorToUse);


	// Print the sorted array
	for (i=0; i &lt; files.length; i++)
	{
	    System.out.println(files[i].getAbsolutePath());
	}

    }




    /**
     * Checks to see if the array of String objects includes an
     * element with the same value as commandSwitch
     *
     * @param args           The array to search
     * @param commandSwitch  The String to search for
     * @return               true if commandSwitch is an element of args
     */
    private static boolean containsSwitch(String[] args, String commandSwitch)
    {
	boolean     contains;
	int         i;

	contains = false;

	i = 0;
	while ((i &lt; args.length) &amp;&amp; (!contains))
	{
	    if (args[i].equals(commandSwitch))
	    {
		contains = true;
	    }

	    i++;
	}

	return contains;
    }
}
</pre></body></html>