C#泛型排序,100分送上----------------------------->

一方晴空 2010-05-18 02:26:14
假如现在有一个Student实体类,按Student里面的Name排序,排序规则如下,Name里面凡是带有“志”这个字的就排在前面。代码最好能比较精炼。
...全文
163 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
一方晴空 2010-05-18
  • 打赏
  • 举报
回复
如果是索引器排序呢?
大Y 2010-05-18
  • 打赏
  • 举报
回复
刚我看的时候还没有人回了,一发,这么多人回了,整的都不好意思了,
大Y 2010-05-18
  • 打赏
  • 举报
回复
看看行不行!
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>");
}

第一回呀,希望能够帮上你的帮,呵呵
一方晴空 2010-05-18
  • 打赏
  • 举报
回复
已经明白了,多谢~
youyou2404 2010-05-18
  • 打赏
  • 举报
回复
不写了
gxingmin 2010-05-18
  • 打赏
  • 举报
回复
那你按6楼的方法可以实现
一方晴空 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gxingmin 的回复:]
重载几个操作符

C# code
public class Student :IComparable<Student>,IComparer<Student>
{
public string Name;

public int CompareTo(Student t)
{
if (this.Name.……
[/Quote]
不用再实体类里面写,主要是外面调用的一个方法
wuyi8808 2010-05-18
  • 打赏
  • 举报
回复
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);
}
}
}
gxingmin 2010-05-18
  • 打赏
  • 举报
回复
重载几个操作符
 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();
}

}
vssvss 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gxingmin 的回复:]
C# code
public class Student :IComparable<Student>,IComparer<Student>
{
public string Name;

public int CompareTo(Student t)
{
if (this.Name.IndexOf(……
[/Quote]
不错 学习了
gxingmin 2010-05-18
  • 打赏
  • 举报
回复
    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);
}
}
tashiwoweiyi 2010-05-18
  • 打赏
  • 举报
回复
类实现IComparable接口
gxingmin 2010-05-18
  • 打赏
  • 举报
回复
类实现IComparable接口

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧