using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompareLab
{
    abstract class Person
    {
        public string Name { get; set; }
        public string Id { get; set; }

        public Person( string name,int ID)
        {
            Name = name;
            Id = ID;

        }

        public abstract  void DisplayDetails( );
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Compare
{
    class Student: Person,IComparable
    {
        public string Course { get; set; }
        public int Year { get; set; }

        public Student( string name,int ID,string course,int year):base(name,ID)
        {
            Course = course;
            Year = year;

        }

        public override void DisplayDetails( )
        {

            Console.WriteLine("{0}{1}         {2}         {3}", Name.PadRight( 15 ), Id, Course, Year);
        }

        public int CompareTo(object obj)
        {

        }
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Compare
{
    class Program
    {
        public static List<Person> DataList = new List<Person>(); 
        static void Main(string[] args)
        {
            bool quit = false;
            do

            {

                var option = ShowMenu( );
                switch ( option )
                {
                    case 1:
                        CreateStudentData( );
                        break;
                    case 2:
                        DisplayStudentData( );
                        break;
                    case 3:
                        DisplayDataByIdReversed( );
                        break;
                    case 4:
                        quit = true;
                        break;
                    default:
                        Console.WriteLine( "Invalid Entry,please try again" );
                        break;

                }

            } while ( !quit );
        }





        public static int ShowMenu( )
        {
            Console.WriteLine( "\t\t1 Create student data"  );
            Console.WriteLine( "\t\t2 Display student data" );
            Console.WriteLine( "\t\t3 Display student data by reverse ID order");
            Console.WriteLine( "\t\t4 Exit" );
            Console.WriteLine("\n");
            //Console.Write("\t\tEnter Option Here: ");
            //var option = Int32.Parse(  Console.ReadLine( ) );
            int option;
            while (true)
            {
                Console.Write("\t\tEnter Option Here: ");
                try
                {
                    option = Int32.Parse(Console.ReadLine());
                    if (option < 1 || option > 4)
                    {
                        throw new Exception();
                    }

                }
                catch
                {
                    Console.WriteLine("Invalid input,must be choice 1-4");
                    continue;
                }
                break;
            }


            return option;


        }
        public static void CreateStudentData()
        {
            DataList.Add(new Student(  "Bob Smith" ,1234,"Software Development",2));
            DataList.Add(new Student("Mary Sue", 1233, "Software Development", 1));
            DataList.Add(new Student("Tom Catello", 7777, "Software Development", 4));
        }

        public static void DisplayStudentData()
        {

            Console.WriteLine("This is a full list :\n");
            Console.WriteLine();
            Console.Write("Name\t\tId\t\tCourse\t\t\tYear\n");
            Console.WriteLine("------------------------------------------------------------------");

            foreach (var value in DataList)
            {

                value.DisplayDetails();
                Console.WriteLine("------------------------------------------------------------------");
                Console.WriteLine();
            }

        }

        public static void DisplayDataByIdReversed()
        {

        }

    }

}

Hi,can you please explain how to use IComparable with the list.I have a List with string name,string module, int id,int year.
How do i use the Icomparable ComapreTo to sort in reverse order?
Regards

Can you show some code? Do you really have to use IComparable, perhaps LINQ is enough:

yourList.OrderByDescending(item => item.Name);

The CompareTo method should return -1, 0 or 1 depending on whether the passed object instance is larger, equal or smaller in order. Have a look at the link I showed, and implement the method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.