subject

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 Call the class you write DirectoryComparator. 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", "dir1", "dir4",

"lab10", "lab2", "lab7", "lab17", "lab8",

"quiz8", "quiz10", "quiz11", "quiz12",

"dir11", "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);

}

}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 16:30
Sara is writing a program to input her monthly phone bills and output the month name and amount for the month with maximum amount. she has defined an array to hold the month names. complete the pseudocode program. [6] # program to output maximum month's phone bill monthname ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"] # define an array to hold the phone bills for each month
Answers: 1
question
Computers and Technology, 21.06.2019 21:30
Ajeweler designing a pin has decided to use five stones chosen from diamonds, rubies, and emeralds. in how many ways can the stones be selected?
Answers: 3
question
Computers and Technology, 22.06.2019 11:00
Which law requires employers to provide safe working environments for their employees? a. civil rights act b. fair labor standards act c. occupational safety and health act d. wagner act
Answers: 1
question
Computers and Technology, 23.06.2019 09:00
The first screen you see when you open word2016 what is called?
Answers: 1
You know the right answer?
It is common for people to name directories as dir1, dir2, and so on. When there are ten or more dir...
Questions
question
Health, 03.09.2021 21:50
question
Mathematics, 03.09.2021 21:50
question
Arts, 03.09.2021 21:50
Questions on the website: 13722363