C#中的sort method()和CompareTo

时间:2014-07-02 01:00:12

标签: c#

我已编写程序并且运行正常,但输出未按说明要求排序。我附上了我的源代码副本。请帮忙......

using System;
using System.Collections.Generic;



namespace FriendListProject
{
    class FriendBirthday
    {
        static void Main()
        {
            List<Friend> friends = new List<Friend>();
            string friendName;
            string friendNumber;
            int friendBirthMonth;
            int friendBirthDay;
            int friendBirthYear;


            Console.WriteLine("For this exercise, you will enter data for just 2 friends.");

            for (int index = 0; index < 2; index++)
            {
                Console.Write("\nEnter the first name of the friend number {0}: ", index + 1);
                friendName = Console.ReadLine();
                Console.Write("Enter the pnone number (aaa-bbbb) of the friend number {0}: ", index + 1);
                friendNumber = Console.ReadLine();
                Console.Write("Enter the birth Month (1-12) of the friend number {0}: ", index + 1);
                friendBirthMonth = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter the birth Day (1-31) of the friend number {0}: ", index + 1);
                friendBirthDay = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter the birth Year (yyyy) of the friend number {0}: ", index + 1);
                friendBirthYear = Convert.ToInt32(Console.ReadLine());


                friends.Add(new Friend(friendName, friendNumber, friendBirthMonth, friendBirthDay, friendBirthYear));

            }

            Console.WriteLine("\nDisplaying the friends' data sorted by first name.");


            for (int index = 0; index < friends.Count; index++)
            {
                Console.WriteLine(friends[index]);
            }

            Console.Write("\nPress the <Enter> key to terminate this program.");
            Console.ReadLine();
        }
    }

    class Friend
    {
        public string FriendName { get; set; }
        public string PhoneNumber { get; set; }
        public int FriendBirthMonth { get; set; }
        public int FriendBirthDay { get; set; }
        public int FriendBirthYear { get; set; }


        public Friend(string FriendName, string PhoneNumber, int MonthOfBirth, int DayOfBirth, int YearOfBirth)
        {
            this.FriendName = FriendName;
            this.PhoneNumber = PhoneNumber;
            this.FriendBirthMonth = MonthOfBirth;
            this.FriendBirthDay = DayOfBirth;
            this.FriendBirthYear = YearOfBirth;
        }

        public override string ToString()
        {
            return string.Format("\nName = {0}. \nPhone = {1}. \nBirthdate = {2}/{3}/{4}.",
                FriendName, PhoneNumber, FriendBirthMonth, FriendBirthDay, FriendBirthYear);
        }


        public int CompareTo(Friend otherFriend, Friend friends)
        {
             return this.FriendName.CompareTo(otherFriend.FriendName);

        }


    }
}

0 个答案:

没有答案