黑马程序员技术交流社区
标题:
对象数组排序
[打印本页]
作者:
秦敖
时间:
2012-10-5 16:50
标题:
对象数组排序
本帖最后由 秦敖 于 2012-10-5 17:00 编辑
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2012/10/5
* Time: 15:28
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using
System;
using
System.Collections;
using
System.Collections.Generic;
namespace
Person对象排序
{
class
Program
{
//将Person对象数组根据年龄从小到大进行排序。
public
static
void
Main
(
string
[] args)
{
Person p1=
new
Person
();
p1.Age=
50
;
p1.Name=
"JM"
;
Person p2=
new
Person
();
p2.Age=
40
;
p2.Name=
"CM"
;
Person p3=
new
Person
();
p3.Age=
30
;
p3.Name=
"DM"
;
Person p4=
new
Person
();
p4.Age=
20
;
p4.Name=
"TM"
;
//以上定义了4个Person对象
//将4个Person对象保存到Person对象数组
Person[] P=
new
Person[]{p1,p2,p3,p4};
//定义一个List<Person>,通过AddRange方法一次性加入4个Person对象。
List<Person> L_person=
new
List<Person>();
L_person.
AddRange
(P);
Console.
WriteLine
(
"---------------数组中Person对象排序前输出------------"
);
Display
(P);
Console.
WriteLine
(
"---------------List<Person>中对象排序前输出------------"
);
Display
(L_person);
Console.
WriteLine
(
"---------------数组中Person对象排序后输出------------"
);
Person_Sort
(P);
Display
(P);
Console.
WriteLine
(
"---------------List<Person>中对象排序后输出------------"
);
Person_Sort
(L_person);
Display
(L_person);
Console.
ReadKey
(
true
);
}
作者:
秦敖
时间:
2012-10-5 17:01
本帖最后由 秦敖 于 2012-10-5 17:04 编辑
///
<summary>
///
Display函数用于对象数组的打印输出,参数类型为Person数组
///
</summary>
///
<param name="P"></param>
static
void
Display
(Person[] P)
{
for
(
int
i=
0
;i<P.Length;i++)
{
Console.
WriteLine
(P
);
}
}
///
<summary>
///
Display函数用于对象数组的打印输出,参数类型为List<Person>,构成重载
///
</summary>
///
<param name="LP"></param>
static
void
Display
(List<Person> LP)
{
for
(
int
i=
0
;i<LP.Count;i++)
{
Console.
WriteLine
(LP
);
}
}
///
<summary>
///
Person_Sort用于Person对象数组的排序,参数类型为Person数组
///
</summary>
///
<param name="P"></param>
static
void
Person_Sort
(Person[] P)
{
Person temp=
new
Person
();
//遍历对象数组,根据对象的Age属性进行冒泡排序。
for
(
int
i=
0
;i<P.Length-
1
;i++)
{
for
(
int
j=
0
;j<P.Length-
1
-i;j++)
{
if
(P[j].Age>P[j+
1
].Age)
{
temp=P[j];
P[j]=P[j+
1
];
P[j+
1
]=temp;
}
}
}
}
///
<summary>
///
Person_Sort用于Person对象数组的排序,参数类型为List<Person>,构成重载
///
</summary>
///
<param name="LP"></param>
static
void
Person_Sort
(List<Person> LP)
{
Person temp=
new
Person
();
//遍历对象数组,根据对象的Age属性进行冒泡排序。
for
(
int
i=
0
;i<LP.Count-
1
;i++)
{
for
(
int
j=
0
;j<LP.Count-
1
-i;j++)
{
if
(LP[j].Age>LP[j+
1
].Age)
{
temp=LP[j];
LP[j]=LP[j+
1
];
LP[j+
1
]=temp;
}
}
}
}
}
///
<summary>
///
定义Person类,具有Age和name属性,覆写了ToString方法用于对象的打印输出。
///
</summary>
class
Person
{
public
int
Age {
get
;
set
;}
public
string
Name{
get
;
set
;}
public
override
string
ToString
()
{
return
Name +
"的年龄是:"
+Age;
}
}
}
求对象数组排序的简洁方式。
作者:
秦敖
时间:
2012-10-6 16:14
using
System;
using
System.Linq;
namespace
Linq实现对象排序
{
class
Program
{
//今天终于找到了一种用Linq实现的排序方式
public
static
void
Main
(
string
[] args)
{
Person p1=
new
Person
(
50
,
"TD"
);
Person p2=
new
Person
(
30
,
"WD"
);
Person p3=
new
Person
(
40
,
"MD"
);
Person p4=
new
Person
(
20
,
"ND"
);
Person[] P=
new
Person[]{p1,p2,p3,p4};
Console.
WriteLine
(
"----------------排序之前------------------"
);
Display
(P);
var
RP = P.
OrderBy
(t=>t.Age);
//用Lamda表达式进行正序排列
Console.
WriteLine
(
"----------------排序之后------------------"
);
foreach
(Person p
in
RP)
{
Console.
WriteLine
(p);
}
Console.
ReadKey
(
true
);
}
///
<summary>
///
实现Person对象数组的输出
///
</summary>
///
<param name="P"></param>
static
void
Display
(Person[] P)
{
for
(
int
i=
0
;i<P.Length;i++)
{
Console.
WriteLine
(P
);
}
}
}
///
<summary>
///
定义一个Person类
///
</summary>
class
Person
{
public
Person
(
int
age,
string
name)
{
Age=age;
Name=name;
}
public
int
Age{
get
;
set
;}
public
string
Name{
get
;
set
;}
public
override
string
ToString
()
{
return
Name+
"的年龄是:"
+Age;
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2