]using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Sample_IComparable_and_IComparer
{
public class StudentComparable
{
public string name;
public int id;
public StudentComparable(string name, int chinese)
{
this.name = name;
this.id=chinese;
}
} class customer
{
public long id;
public string name;
public customer(long id, string name)
{
this.id = id;
this.name = name;
}
} public class StudentComparer : IComparer
{ int IComparer.Compare(object x, object y)
{ StudentComparable left = (StudentComparable)x;
StudentComparable right = (StudentComparable)y; return Compare(left, right); } public int Compare(StudentComparable left, StudentComparable right)
{
return left.id.CompareTo(right.id);
} } public class TestClass
{
public static void Main()
{
StudentComparable[] student = new StudentComparable[4];
student[0] = new StudentComparable("C", 60);
student[1] = new StudentComparable("A", 70);
student[2] = new StudentComparable("D", 90);
student[3] = new StudentComparable("B", 80);
Console.WriteLine("\n\n***** IComparer 根据语文成绩排序 *****\n"); StudentComparer sc2 = new StudentComparer();
Array.Sort(student, sc2);
foreach (StudentComparable s in student)
{
Console.WriteLine(s.id);
}
Console.Read(); }
}}
我所遇到的问题是如果StudentComparer 不继承IComparer接口,并且在类内不实现IComparer.Compare方法,
大家可以测试仍然可以得到结果
我看到一个例子它用Array.Sort(student, sc2.Compare)排序确也可以得到结果,那干嘛还用继承接口,
并且sc2.Compare返回值是什么,因为没有Array.Sort(array,int)的重载,为什么可以这样用呢?谢谢
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Sample_IComparable_and_IComparer
{
public class StudentComparable
{
public string name;
public int id;
public StudentComparable(string name, int chinese)
{
this.name = name;
this.id=chinese;
}
} class customer
{
public long id;
public string name;
public customer(long id, string name)
{
this.id = id;
this.name = name;
}
} public class StudentComparer : IComparer
{ int IComparer.Compare(object x, object y)
{ StudentComparable left = (StudentComparable)x;
StudentComparable right = (StudentComparable)y; return Compare(left, right); } public int Compare(StudentComparable left, StudentComparable right)
{
return left.id.CompareTo(right.id);
} } public class TestClass
{
public static void Main()
{
StudentComparable[] student = new StudentComparable[4];
student[0] = new StudentComparable("C", 60);
student[1] = new StudentComparable("A", 70);
student[2] = new StudentComparable("D", 90);
student[3] = new StudentComparable("B", 80);
Console.WriteLine("\n\n***** IComparer 根据语文成绩排序 *****\n"); StudentComparer sc2 = new StudentComparer();
Array.Sort(student, sc2);
foreach (StudentComparable s in student)
{
Console.WriteLine(s.id);
}
Console.Read(); }
}}
我所遇到的问题是如果StudentComparer 不继承IComparer接口,并且在类内不实现IComparer.Compare方法,
大家可以测试仍然可以得到结果
我看到一个例子它用Array.Sort(student, sc2.Compare)排序确也可以得到结果,那干嘛还用继承接口,
并且sc2.Compare返回值是什么,因为没有Array.Sort(array,int)的重载,为什么可以这样用呢?谢谢
解决方案 »
- linq Lambda表达式中时间比较
- 京东商城上的商品筛选是怎么做的
- 求助:session问题!!!
- vs上传组件放updatepanel里面就不起作用,怎么解决?
- vs2005下同一个项目里怎么不能使用两个.cs文件(两个cs文件里都有Main函数)
- 关于.net2.0下面的serialport类的问题
- 双机通信规定前 10 个字节是命令 ,如果保证有10个固定的字节。
- vs2008 c#开发windows窗体程序,怎么连接数据库?
- 最近在学习C#有好多知识都不了解,请各路高手帮忙指点(CategoryModel.cs)
- 怎么样得到网络中任意一台机器的硬件信息和系统信息?
- OnTextChanged不被触发的问题
- VS简单问题:怎么新建事件,例如单击窗体?
这个时候是使用sc2.Compare这个函数进行比较
Array.Sort(student, sc2);这个时候是使用IComparer这个接口进行比较操作的这两种方法相对来说灵活一些.可以更改不同的比较对象来使用不同的排序规则,
比如你提供了3个一个是按照成绩,一个是按照用户名称,一个是按照用户名称的长度
Array.Sort(student);这个时候如果StudentComparable从IComparer接口继承过来,那么就也可以使用这种方式.
这属于传统方式
继承了 IComparer就需要实现int IComparer.Compare(object x, object y)方法,
但是由于我们实例化此时会产生装箱与拆箱,所以定义了一个
Compare(StudentComparable left, StudentComparable right)方法,
而两种比较方式不同的是
前者不能修改只能用事先安排的字段来排序,而后者由于array.sort(,new )具有两个参数,
第二个参数就可以传递你所需要排序的字段,我想这就是两者最大的区别。
import java.util.ArrayList; public class Demo{
public static void main(String[] args){
User u1 =new User("aaa",12);
User u2 =new User("ddd",10);
User u3 =new User("ccc",20);
User u4 =new User("bbb",10);
ArrayList arrayList = new ArrayList();
arrayList.add(u1);
arrayList.add(u2);
arrayList.add(u3);
arrayList.add(u4);
Object[] users =arrayList.toArray();
System.out.println ("排序前。");
for (int i = 0; i<users.length; i++){
System.out.println (users[i]);
}
System.out.println ("*******************************");
System.out.println ("排序后。。");
//把排序规则交给sort方法。该方法就回按照你自定义的规则进行排序
java.util.Arrays.sort(users,new MyComparator());
for (int i = 0; i<users.length; i++){
System.out.println (users[i]);
}
}
}class User{
String name;
int age ;
public User(String name,int age){
this.name = name;
this.age = age;
}
public String toString(){
return name+":::"+age;
}
}
class MyComparator implements Comparator{
public int compare(Object obj1, Object obj2 ){
User u1 = (User)obj1;
User u2 = (User)obj2;
if(u1.age>u2.age){
return 1;
}else if(u1.age<u2.age){
return -1;
}else{
//利用String自身的排序方法。
//如果年龄相同就按名字进行排序
return u1.name.compareTo(u2.name);
}
}
}