Following is the answer:
import java.util.Arrays; import java.util.Comparator; public class main { public static String[] sort(String[] paths){ class PathComparator implements Comparator<String>{ public int compare(String p1, String p2){ if(countSlashes(p1) == countSlashes(p2)){ return p1.compareTo(p2); }else { return countSlashes(p1) - countSlashes(p2); } } private int countSlashes(String p){ return p.length() - p.replace("/","").length(); } } Arrays.sort(paths,new PathComparator()); return paths; } public static void main(String[] args) { String[] str = {"/home/me/cs1", "/user/share", "/var/log", "/user/local/jdk1.6.0/jre/lib"}; for(int i = 0 ; i < str.length ; i++){ System.out.println(sort(str)[i]); } } }
Output:
Your task is to sort an array list of path names such as /home/me/cs1 /usr/share /var/log...
JAVAFX ONLY PROGRAM!!!!!
SORTING WITH NESTED CLASSES AND LAMBDA
EXPRESSIONS.
DIRECTIONS ARE BELOW:
DIRECTIONS:
The main point of the exercise is to demonstrate your
ability to use various types of nested classes. Of course, sorting
is important as well, but you don’t really need to do much more
than create the class that does the comparison. In general, I like
giving you some latitude in how you design and implement your
projects. However, for this assignment, each piece is very...
Your running times will probably be different than these. Please
do a better job with the snipping tool than I did.
Java program provided:
// Student Name Today's Date
import java.util.Arrays;
import java.util.Random;
public class SortTimer {
// Please expand method main() to meet the lab
requirements.
// You have the following sorting methods
available:
// insertionSort(int[] a);
// selectionSort(int[] a);
// mergeSort(int[] a);
// quickSort(int[] a);
// The array will be in sorted order after the
routines are called!
...