Question

Write a program in C# to display the list of items in the array according to...

Write a program in C# to display the list of items in the array according to the length of the string in ascending order and the original order. The array should contain the following data “ROME, LONDON, NAIROBI, CALIFORNIA, ZURICH, NEW DELHI, AMSTERDAM, HOUSTON, PARIS". The output should be as follow

Here is the arranged list:                                                                                  

ROME                                                                                                       

PARIS                                                                                                      

LONDON                                                                                                       

ZURICH

HOUSTON                                                                                            

NAIROBI                                                                                                      

AMSTERDAM                                                                                                    

NEW DELHI                                                                                                    

CALIFORNIA

Here is the original list

ROME

LONDON

NAIROBI

CALIFORNIA

ZURICH

NEW DELHI

AMSTERDAM

HOUSTON

PARIS

Homework Answers

Answer #1

If you have any doubts, please give me comment...

using System;

class Excercise {

    public static void Main () {

        string[] items = { "ROME", "LONDON", "NAIROBI", "CALIFORNIA", "ZURICH", "NEW DELHI", "AMSTERDAM", "HOUSTON", "PARIS" };

        string[] sortedItems = (string[]) items.Clone ();

        for (int i = 0; i < sortedItems.Length; i++) {

            for (int j = 0; j < sortedItems.Length-i-1; j++) {

                int len1 = sortedItems[j].Length;

                int len2 = sortedItems[j+1].Length;

                if (len1 > len2 || (len1==len2 && sortedItems[j].CompareTo(sortedItems[j+1])>0)) {

                    string temp = sortedItems[j];

                    sortedItems[j] = sortedItems[j+1];

                    sortedItems[j+1] = temp;

                }

            }

        }

        Console.WriteLine ("Here is the arranged list:");

        for (int i = 0; i < sortedItems.Length; i++) {

            Console.WriteLine (sortedItems[i]);

        }

        Console.WriteLine ("Here is the original list:");

        for (int i = 0; i < items.Length; i++) {

            Console.WriteLine (items[i]);

        }

    }

}

Know the answer?
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
Similar Questions
An array of characters contains a few letters. Write a complete C program that will display...
An array of characters contains a few letters. Write a complete C program that will display the output as shown below. Lets assume the array contains =”abcde”. The program should be able to work with any array size, configurable in the program. Expected output Original array = [ a b c d e] a a b a b c a b c d a b c d e
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
Please write code in C. Thank you! Write a program that reads a list of integers,...
Please write code in C. Thank you! Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. You can assume that the list will have at least 2 integers and less than 20 integers. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 To achieve the above, first read...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...
Write a program that reads an integer, a list of words, and a character. The integer...
Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle z then...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
4. Write a C++ program that reads Id, name, and GPA of each of n students...
4. Write a C++ program that reads Id, name, and GPA of each of n students in a course, where n is an integer number between 10 and 30 (inclusive). Use bubble sorting to display the list of students in an ascending order according to the student name.
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Answer question for C# 1 of 10 When items are added or removed, a list resizes...
Answer question for C# 1 of 10 When items are added or removed, a list resizes itself. True False Question 2 of 10 What happens when you assign one list to another using the assignment operator? An error message generates. Elements copy to new memory locations. Both lists become value-type data elements. Both lists reference the same memory locations. Question 3 of 10 Which of the following is a valid example of calling a method and sending it an array...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT