Question

Your task is to sort an array list of path names such as /home/me/cs1 /usr/share /var/log /usr/local/jdk1.6.0/jre/lib Sort byPathSorter.java 1 2 import java.util.Arrays; import java.util.Comparator; public class PathSorter public String[] sort(String

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

/user/share /var/log /home/me/cs1 /user/local/jdk1.6.0/jre/lib

Add a comment
Know the answer?
Add Answer to:
Your task is to sort an array list of path names such as /home/me/cs1 /usr/share /var/log...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main...

    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. Jav...

    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!   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT