假如现在有一个Student实体类,按Student里面的Name排序,排序规则如下,Name里面凡是带有“志”这个字的就排在前面。代码最好能比较精炼。
解决方案 »
- 如何在Mainpage和SecondPage中通信
- 如何截取以换行符(转行符)作为划分的字符串?
- new和方法的问题~~~
- 请教用C#或VB.net如何开发视频播放程序,如何对mpeg等格式进行编码?
- 关于使用notifyIcon问题
- vs2005 sp1 win7安装
- 高手帮忙! SQL SERVER中数据类型为smalldatatime的数据怎么读?我按照varchar的方法读出来没法显示啊!~
- vs.net2003新建项目出错
- 请教基础知识 :线程的工作原理、google的检索原理
- XML是如何封装图像文件的?
- 一个比较郁闷的替换问题
- SQL在插入1条记录后 怎样使其他为null的字段值为0 怎样写表的触发器
{
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 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();
}
}
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
{
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>");
}第一回呀,希望能够帮上你的帮,呵呵