8
Answers

Sorting of Array Object ?

Keith

Keith

16y
2.7k
1

Below is a working program, can anyone help me to sort according to the Bus number.
Btw, do I have to apply Array.Sort ? Pls advise, thanks.

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Bus
    {
        int busNo;
        string startDepot, endDepot;
        float departTime;

        public void Update()
        {
            Console.Write("Entert Bus No.:");
            busNo = int.Parse(Console.ReadLine());
            Console.Write("Enter Start depot: ");
            startDepot = Console.ReadLine();
            Console.Write("Enter End depot: ");
            endDepot = Console.ReadLine();
            Console.Write("Enter depart time: ");
            departTime = float.Parse(Console.ReadLine());

        }

        public void Display()
        {
            Console.WriteLine("Bus No. is " + busNo);
            Console.WriteLine("Start depot is " + startDepot);
            Console.WriteLine("End depot is " + endDepot);
            Console.WriteLine("Depart time is " + departTime);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            char choice;
            Bus[] group1 = null;
            int num = 0;


            do
            {
               
                Console.WriteLine("a) Update   b) Display  e) Exit");
                Console.Write("Enter choice: ");
                choice = char.Parse(Console.ReadLine());
               
                switch (choice)
                {
                    case 'a':
                        Console.WriteLine("Enter Number of Bus");
                        num = int.Parse(Console.ReadLine());
                        group1 = new Bus[num]; //dynamic sizing

                        for (int row = 0; row < num; row++)
                        {
                            group1[row] = new Bus();
                            group1[row].Update();
                        }
                        break;

                    case 'b':
                        Console.WriteLine("Display all Bus(es)");
                       
                        for (int row = 0; row < num; row++)
                        {
                            //group1[row] = new Bus();
                            group1[row].Display(); //stuck
                        }
                        break;

                    default: Console.WriteLine("No such choice"); break;
                }

            } while (choice != 'e');

        }
    }
}.

 

Answers (8)