加载中…
个人资料
一江春水
一江春水
  • 博客等级:
  • 博客积分:0
  • 博客访问:881
  • 关注人气:13
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C# SortedSet的用法

(2013-09-28 14:42:03)
标签:

学习总结

it

分类: C#

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Collections;
using conapp2.dll;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace conapp2
{
    class Program
    {
        static void Main(string[] args)
        {


            SortedSet list = new SortedSet();
            Student st = new Student("andy", 25, 100);
            list.Add(st);

            //st = new Student("andy", 25, 100);
            //list.Add(st);

            st = new Student("jack", 70, 60);
            list.Add(st);

            st = new Student("andy", 25, 50);
            list.Add(st);

            st = new Student("lucy", 60, 80);
            list.Add(st);

            st = new Student("tom", 40, 40);
            list.Add(st);

            foreach (Student stu in list)
            {
                Console.WriteLine(stu.Name+"\t"+stu.Age+"\t"+stu.Score);
            }

            Console.ReadLine();
        }
    }
    ///
    /// 生命一个Student类(该类还要继承IComparable借口)
    ///
    class Student : IComparable
    {
        //定义三个属性
        public string Name { get; set; }
        public int Age { get; set; }
        public float Score { get; set; }
        //对属性进行初始化
        public Student(string name, int age, float score)
        {
            this.Name = name;
            this.Age = age;
            this.Score = score;
        }
        ///
        /// 实现IComparable借口中CompareTo的方法
        ///
        ///
        ///
        public int CompareTo(Student y)
        {
            if (this.Score > y.Score)//先按分数进行排序
            {
                return 1;
            }
            else if (this.Score < y.Score)
            {
                return -1;
            }
            else
            {
                if (this.Name.CompareTo(y.Name) > 0)//分数相同的情况下再按姓名
                {
                    return 1;
                }
                else if (this.Name.CompareTo(y.Name) < 0)
                {
                    return -1;
                }
                else
                {
                    if (this.Age >= y.Age)//在前两者都相同的情况下就按年龄排序
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
            }
        }
    }
   
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有