110,536
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}
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();
}
}
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);
}
}