假如现在有一个Student实体类,按Student里面的Name排序,排序规则如下,Name里面凡是带有“志”这个字的就排在前面。代码最好能比较精炼。

解决方案 »

  1.   

    类实现IComparable接口 
      

  2.   

        public class Student :IComparable<Student>,IComparer<Student>
        {
            public string Name;        public int CompareTo(Student t)
            {
                if (this.Name.IndexOf("志")>-1 && t.Name.IndexOf("志")>-1)
                    return this.Name.CompareTo(t.Name);
                if (this.Name.IndexOf("志")>-1)
                    return -1;
                if (t.Name.IndexOf("志")>-1)
                    return 1;            return this.Name.CompareTo(t.Name);
            }        public int Compare(Student a, Student b)
            {
                return a.CompareTo(b);
            }
        }
      

  3.   

    重载几个操作符
     public class Student :IComparable<Student>,IComparer<Student>
        {
            public string Name;        public int CompareTo(Student t)
            {
                if (this.Name.IndexOf("志")>-1 && t.Name.IndexOf("志")>-1)
                    return this.Name.CompareTo(t.Name);
                if (this.Name.IndexOf("志")>-1)
                    return -1;
                if (t.Name.IndexOf("志")>-1)
                    return 1;            return this.Name.CompareTo(t.Name);
            }        public int Compare(Student a, Student b)
            {
                return a.CompareTo(b);
            }        public static bool operator >(Student a, Student b)
            {
                return (a.CompareTo(b) > 0);
            }        public static bool operator <(Student a, Student b)
            {
                return (a.CompareTo(b) < 0);        }        public static bool operator >=(Student a, Student b)
            {
                return (a.CompareTo(b) >= 0);
            }        public static bool operator <=(Student a, Student b)
            {
                return (a.CompareTo(b) <= 0);        }        public static bool operator ==(Student a, Student b)
            {
                return (a.CompareTo(b) == 0);
            }        public static bool operator !=(Student a, Student b)
            {
                return (a.CompareTo(b) != 0);        }        public override bool Equals(object obj)
            {
                if (obj == null)
                    return false;
                if (obj as Student == null)
                    return false;            return this == obj as Student;
            }        public override int GetHashCode()
            {
                return this.Name.GetHashCode();
            }
        
        }
      

  4.   

    using System;
    using System.Collections.Generic;class Student
    {
      private string name;
      public string Name { get { return name; } }
      public Student(string name) { this.name = name; }
      public override string ToString() { return name; }
    }class Program
    {
      private static int CompareByNameZhi(Student x, Student y)
      {
        if (x == null)
        {
          if (y == null) return 0;
          else return -1;
        }
        else // x != null
        {
          if (y == null) return 1;
          else
          {
            if (x.Name == y.Name) return 0;
            return y.Name.IndexOf('志') - x.Name.IndexOf('志');
          }
        }
      }
      
      static void Main()
      {
        List<Student> list = new List<Student>();
        list.Add(new Student("张三"));
        list.Add(new Student("志在必得"));
        list.Add(new Student("李四"));
        list.Add(new Student("杨志"));
        list.Sort(CompareByNameZhi);
        foreach (Student x in list)
        {
          Console.WriteLine(x);
        }
      }
    }
      

  5.   

    看看行不行!
        public class student
        {
            public  string Name;
            public  string Gender;
        }页面里面写
            List<student> students = new List<student>
                {
                 new  student { Name="是男人", Gender="男" },
                 new  student { Name="男人", Gender="男" },
                 new  student { Name="是", Gender="男" },
                 new  student { Name="人", Gender="男" },
                 new  student { Name="是打开的附件人", Gender="男" },
                 new  student { Name="地方", Gender="男" },
                 new  student { Name="都是人废物", Gender="男" },
                 new  student { Name="出色的额", Gender="男" }            };        var resultName = from studentInfo in students
                             where studentInfo.Name.Contains("是")//检索出字为"是"的
                             orderby studentInfo.Name ascending 
                             select studentInfo.Name;        foreach (var item in resultName)  //输出
            {
                Response.Write(item + "<br>");           
            }第一回呀,希望能够帮上你的帮,呵呵