user1780149
user1780149

Reputation: 53

Lists and Sorting

Basically what I am trying to do here is to create my own struct and utilize it by taking user input, adding it into a list, and then sorting it in different ways(ID and such).

I think I made the struct correctly, but I cannot figure out how to compare these 2 student instances, sort them by ID, and print them out(in sorted fashion by ID) to the console.

Any ideas? I think I'm going in the right direction.

   namespace App26
{
    public struct Student
    {
        public String first, last;
        public double ID;

        public Student(String first, String last, double ID)
        {
            this.first = first;
            this.last = last;
            this.ID = ID;
        }
    }

    class IDCompare : IComparer<Student>
    {
        public int Compare(Student a, Student b)
        {
            return a.first.CompareTo(b.f);
        }
    }


    class Program
    {

        static void Main(string[] args)
        {
            String firstname, lastname, first, last;
            double num, IDnum;

            //First person   
            Console.WriteLine("Please enter first name");
            firstname = Console.ReadLine();
            Console.WriteLine("Please enter last name");
            lastname = Console.ReadLine();
            Console.WriteLine("Please enter ID");
            IDnum = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine();

            //Second Person
            Console.WriteLine("Please enter first name");
            first = Console.ReadLine();
            Console.WriteLine("Please enter last name");
            last = Console.ReadLine();
            Console.WriteLine("Please enter ID");
            num = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine();

            List<Student> list = new List<Student>();
            Student person1 = new Student(firstname, lastname, IDnum);
            //Student person2 = new Student(first, last, num);
            list.Add(person1);
            list.Add(person2);
            list.Sort();

            foreach (Student i in list)
            Console.WriteLine(i);
        }        
       }
}

Upvotes: 1

Views: 359

Answers (3)

Matthias
Matthias

Reputation: 5764

You can also use Linq very easily. Then you don't need to implement any comparer.

foreach (Student entry in myList.OrderBy(student => student.ID))
  Console.Write(entry.Name);

Sorting by a second property is also available. You can also use descending sort.

foreach (Student entry in myList.OrderBy(student => student.ID).ThenBy(student => student.Name))
  Console.Write(entry.Name);

Upvotes: 1

Nikola Davidovic
Nikola Davidovic

Reputation: 8666

Your IDCompare is not comparing IDs but first (I presume names). You should change it like this:

class IDCompare : IComparer<Student>
{
    public int Compare(Student a, Student b)
    {
        return a.ID.CompareTo(b.ID);
    }
}

And then call your sort like this:

list.Sort(new IDCompare());

Have in mind that IDs are usually integers, not doubles (although I don't know what your ID means).

If you want to use this

 foreach (Student i in list)
            Console.WriteLine(i);

I advise you to override ToString() method in your struct (why don't you use a class?)

public override string ToString()
{
       return ID.ToString() + " " + first + " " + last + " ";
}

Upvotes: 1

Ravi Y
Ravi Y

Reputation: 4376

You should use the code below for sorting

list.Sort(new IDCompare()); 

Also

return a.first.CompareTo(b.f);

seems to be incorrect. Please check your code compile time errors as well.

Upvotes: 2

Related Questions