Question

In: Computer Science

You are provided with an array of Strings and a list of Strings. Sort the elements...

You are provided with an array of Strings and a list of Strings. Sort the elements (1) in natural order, (2) in reverse natural order and (3) by the length of each String. You can fill in the details by using the following stub file:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Midterm01 {
    public static void main(String[] args) {
        String[] arrayOfCities = { "Atlanta", "Savannah", "New York", "Dallas", "Rio" };
        List<String> listOfCities = Arrays.asList("Atlanta", "Savannah", "New York", "Dallas", "Rio");

        System.out.print("\nSorting a String array in natural order...");
        // your work here

        System.out.print("\nSorting a String array in reverse natural order...");
        // your work here

        System.out.print("\nSorting a String array by length of the Strings...");
        // your work here

        System.out.print("\nSorting a String list in natural order...");
        // your work here

        System.out.print("\nSorting a String list in reverse natural order...");
        // your work here

        System.out.print("\nSorting a String list by length of Strings...");
        // your work here
    }
}

Solutions

Expert Solution

This is the code

import java.util.Collections;
import java.util.Arrays;
import java.util.List;

public class ProgrammingProblem1{
        public static void main(String[] args) {
                String[] arrayOfCities = { "Atlanta", "Savannah", "New York", "Dallas", "Rio" };
                List<String> listOfCities = Arrays.asList("Atlanta", "Savannah", "New York", "Dallas", "Rio");
                
                System.out.print("\nSorting a String array in natural order...");
                // your work here
                Arrays.sort(arrayOfCities);
                System.out.println(Arrays.toString(arrayOfCities));
                
                System.out.print("\nSorting a String array in reverse natural order...");
                // your work here
                Arrays.sort(arrayOfCities, Collections.reverseOrder());
                System.out.println(Arrays.toString(arrayOfCities));
                
                System.out.print("\nSorting a String array by length of the Strings...");
                // your work here
                Arrays.sort(arrayOfCities, (s1, s2)->Integer.compare(s1.length(), s2.length()));
                System.out.println(Arrays.toString(arrayOfCities));
                
                System.out.print("\nSorting a String list in natural order...");
                // your work here
                Collections.sort(listOfCities);
                System.out.println(listOfCities);
                
                System.out.print("\nSorting a String list in reverse natural order...");
                // your work here
                Collections.sort(listOfCities, Collections.reverseOrder());
                System.out.println(listOfCities);
                
                System.out.print("\nSorting a String list by length of Strings...");
                // your work here
                Collections.sort(listOfCities, (s1, s2)->Integer.compare(s1.length(), s2.length()));
                System.out.println(listOfCities);
        }
}

And this is the output that i got

Hope this will be helpful


Related Solutions

The elements of the list are going to be strings, and will be in order of...
The elements of the list are going to be strings, and will be in order of strictly increasing or decreasing length. Some examples of lists in strictly increasing length order are [“pan”, “banana”,”xylophone”], and [“kayn”, “swain”, “morgana”].   Function Name: fix_string_list Parameter: data - A list with 3 elements (that are all lowercase strings) that is either in order of strictly increasing or decreasing length. Return: A list with the same 3 elements as the parameter that is in order of...
Write an ARMv8 program to sort an array of elements. As Imentioned in class, this...
Write an ARMv8 program to sort an array of elements. As I mentioned in class, this problem uses a portion of your Programming Assignment 1 where you computed the smallest and largest values in an array. Here we will extend that assignment to find the “index” of the smallest and the index of the largest elements of the array. The following C code segment illustrates how we can sort the element of an array.For this problem, assume an array with...
Consider an array of 10 elements sorted in ascending order. Assume that you sort the table...
Consider an array of 10 elements sorted in ascending order. Assume that you sort the table in descending order by running a sorting algorithm. a) Count the exact number of changes made to the array (i.e. the number of operations of assignment T [...]=) if the bubble sort is applied (Assuming two assignments per swap). b) Count the exact number of changes made to the array (i.e. the number of operations of assignment T [...]=) if heap-sort is applied (Assuming...
i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
Write a Python program containing a function named scrabble_sort that will sort a list of strings...
Write a Python program containing a function named scrabble_sort that will sort a list of strings according to the length of the string, so that shortest strings appear at the front of the list. Words that have the same number of letters should be arranged in alphabetical order. Write your own logic for the sort function (you may want to start from some of the existing sorting code we studied). Do NOT use the built-in sort function provided by Python....
Using the worst case scenario for a recursive insertion sort on an array of 5 elements...
Using the worst case scenario for a recursive insertion sort on an array of 5 elements {5, 4, 3, 2, 1} Determine a formula that counts the numbers of nodes in that recursion tree. What is the Big O for execution time. Determine a formula that expresses the height of the recursion tree. What is the Big O for memory?
. Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list...
. Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list of at least 5 integers (b) Prints the list before and after sorting.
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. (Write a C# program)
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. C++ program thanks
Change the code to sort list of strings without using any pre-defined functions Here is my...
Change the code to sort list of strings without using any pre-defined functions Here is my code: int n; String temp; Scanner in = new Scanner(System.in); System.out.print("Enter number of names you want to enter:"); n = in.nextInt(); String names[] = new String[n]; Scanner s1 = new Scanner(System.in); System.out.println("Enter all the names:"); for(int i = 0; i < n; i++){ names[i] = s1.nextLine(); } ***CHANGE THIS PART*** for (int i = 0; i < n; i++){ for (int j = i...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT