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...
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
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)
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...
I need to write a method that sorts a provided Linked list with bubble sort and...
I need to write a method that sorts a provided Linked list with bubble sort and using ONLY Java List Iterator Methods (Link Below) https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html     public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {     // first line to start you off     ListIterator<E> iit = c.listIterator(), jit;     /**** Test code: do not modify *****/     List cc = new LinkedList(c);     Collections.sort(c);     ListIterator it1 = c.listIterator(), it2 = cc.listIterator(); while (it1.hasNext()) { if (!it1.next().equals(it2.next()))         throw new Exception("List not sorted");...
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array...
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3: Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3:
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for...
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for string in strings: st = "f'" + string + ": {" + string + "}'" print_stmt = 'print(' + st + ', end="; ")' # Uncomment the following print statement to see the statement to be executed. # Each will appear on a separate line. # print(f'\n{print_stmt}') exec(print_stmt) print() print_lv(['list(r)', 'r[-2:3:-1]', 'list(r[-2:3:-1])']) OUTPUT: list(r): [10, 13, 16, 19, 22, 25, 28, 31, 34, 37];...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT