Array类的Sort()方法
来源:互联网 发布:军事题材电视剧知乎 编辑:程序博客网 时间:2024/05/14 17:42
Array类实现了数组中元素的冒泡排序。Sort()方法要求数组中的元素实现IComparable接口。如System.Int32
和System.String实现了IComparable接口,所以下面的数组可以使用Array.Sort()。
C# Code
2
3
4
5
6
string[] names = { "Lili", "Heicer", "Lucy" };
Array.Sort(names);
foreach (string n in names)
{
Console.WriteLine(n);
}
输出排序后的数组:Array.Sort(names);
foreach (string n in names)
{
Console.WriteLine(n);
}
如果对数组使用定制的类,就必须实现IComparable接口。这个借口定义了一个方法CompareTo()。
Person类:
C# Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Person : IComparable
{
public Person() { }
public Person(string name, string sex)
{
this.Name = name;
this.Sex = sex;
}
public string Name;
public string Sex;
public override string ToString()
{
return this.Name + " " + this.Sex;
}
#region IComparable 成员
public int CompareTo(object obj)
{
Person p = obj as Person;
if (p == null)
{
throw new NotImplementedException();
}
return this.Name.CompareTo(p.Name);
}
#endregion
}
{
public Person() { }
public Person(string name, string sex)
{
this.Name = name;
this.Sex = sex;
}
public string Name;
public string Sex;
public override string ToString()
{
return this.Name + " " + this.Sex;
}
#region IComparable 成员
public int CompareTo(object obj)
{
Person p = obj as Person;
if (p == null)
{
throw new NotImplementedException();
}
return this.Name.CompareTo(p.Name);
}
#endregion
}
C# Code
2
3
4
5
6
7
8
9
10
11
Person[] persons =
{
new Person("Lili", "Female"),
new Person("Heicer", "Male"),
new Person("Lucy", "Female")
};
Array.Sort(persons);
foreach (Person p in persons)
{
Console.WriteLine(p);
}
{
new Person("Lili", "Female"),
new Person("Heicer", "Male"),
new Person("Lucy", "Female")
};
Array.Sort(persons);
foreach (Person p in persons)
{
Console.WriteLine(p);
}
如果Person对象的排序方式不同,或者不能修改在数组中用作元素的类,就可以执行ICompare接口。这个接口定义了Compare()方法。ICompare接口必须要独立于要比较的类。这里定义PersonCompare类
PersonCompare类:
C# Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class PersonComparer: IComparer
{
public PersonComparer() { }
#region IComparer 成员
public int Compare(object x, object y)
{
Person p1 = x as Person;
Person p2 = y as Person;
if (p1 == null || p2 == null)
{
throw new ArgumentException("Person为空");
}
return p1.Name.CompareTo(p2.Name);
}
#endregion
}
{
public PersonComparer() { }
#region IComparer 成员
public int Compare(object x, object y)
{
Person p1 = x as Person;
Person p2 = y as Person;
if (p1 == null || p2 == null)
{
throw new ArgumentException("Person为空");
}
return p1.Name.CompareTo(p2.Name);
}
#endregion
}
C# Code
Array.Sort(persons, new PersonComparer());
结果是就不输出了。
另外Sort()方法也可以把委托作为参数:
C# Code
pulic delegate int Comparison<T>(T x, T y);
C# Code
2
3
4
Array.Sort(persons, delegate(Person p1, Person p2)
{
return p1.Name.CompareTo(p2.Name);
});
{
return p1.Name.CompareTo(p2.Name);
});
C# Code
Array.Sort(persons, (p1, p2) => p1.Name.CompareTo(p2.Name));
0 0
- Array类的Sort()方法
- js模拟实现Array的sort方法
- Array.sort(function(){})方法的调用
- [ javascript ] js中array的sort()方法!
- Array.sort()内部使用的排序方法
- array的sort和binarySearch方法
- Array.Sort方法
- Array.sort自定义方法
- javascript 中 Array.sort()方法
- javascript中Array.sort()方法
- js中关于array的slice和sort方法
- 使用Array的Sort()方法对数组进行排序
- Javascript数组(array) sort方法的解释和分析
- Java Array.sort()两种重载的排序方法
- Java Array.sort()两种重载的排序方法
- [前端面试]数组排序:array.sort()方法的理解~
- javascript 利用Array的sort方法,对Array进行排序 (Array里面放的是对象而不是字符串)
- 数组Array的排序sort
- postgresql hstore用法
- View工作原理(三)视图大小计算过程(measure过程)
- 从后台返回游戏的时候,新增弹出窗口的图片在android不加载(纹理变黑)的解决办法
- Java IO _压缩流
- UVA 656 - Optimal Programs(BFS)
- Array类的Sort()方法
- 黑马程序员_java多线程总结
- javascript目录
- [CSDN]我的2013,好久不见
- Mahout入门安装
- FileSync Eclipse的文件同步插件
- 黑马程序员_JavaScript、document
- 算法竞赛入门经典-课后练习-3-1-2分数统计
- python logging 日志记录