泛型排序 问题

houzy123 2009-12-18 07:48:34
问题如下
现要对一组数据(name,age,score)进行排序处理,并分别对age,score进行排序
public class Person : IComparable<Person>
{
string name;
int age;
int score;
public Person(string s, int i,int sc)
{
name = s;
age = i;
score = sc;
}
public int CompareTo(Person p)
{
return age - p.age;//现在可以对age进行排序,下一句对score排序,但无法调用
// return score - p.score;
}
public override string ToString()
{
return name + ":" + age+" scroe:"+score;
}
public bool Equals(Person p)
{
return (this.age == p.age || this.score==p.score);
}
}
// 尖括号中的类型参数 T。
public class MyList<T> : IEnumerable<T>
{
protected Node head;
protected Node current = null;

protected class Node
{
public Node next;
private T data;
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
public T Data
{
get { return data; }
set { data = value; }
}
}

public MyList()
{
head = null;
}
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
public IEnumerator<T> GetEnumerator()
{
Node current = head;

while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

}

public class SortedList<T> : MyList<T> where T : IComparable<T>
{
public void BubbleSort()
{
if (null == head || null == head.Next)
return;

bool swapped;
do
{
Node previous = null;
Node current = head;
swapped = false;

while (current.next != null)
{
if (current.Data.CompareTo(current.next.Data) > 0)
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current;

if (previous == null)
{
head = tmp;
}
else
{
previous.next = tmp;
}
previous = tmp;
swapped = true;
}

else
{
previous = current;
current = current.next;
}

}// 结束 while
} while (swapped);
}
}

static void Main(string[] args)
{
SortedList<Person> list = new SortedList<Person>();
string[] names = new string[] { "Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul" };
int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };
int[] scores = new int[] { 90, 40, 28, 23, 18, 56, 108, 44, 30, 23 };

for (int x = 0; x < names.Length; x++)
{
list.AddHead(new Person(names[x], ages[x],scores[x]));
}
// 对列表进行排序。
list.BubbleSort();
}
...全文
277 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qldsrx 2009-12-21
  • 打赏
  • 举报
回复
SortedList 是系统自带的一个类,你就算要自己写一个,也别用同名啊。其实完全可以用系统的排序类。
wuyq11 2009-12-21
  • 打赏
  • 举报
回复
public class Person Comparer : IComparer<Person >
{
public enum CompareType
{
Name,
Age
}

private CompareType type;
public StudentComparer(CompareType type)
{
this.type = type;
}
public int Compare(Person x, Person y)
{
switch(this.type)
{
case CompareType.Name:
return x.Name.CompareTo(y.Name);
case CompareType.Age:
return x.Age.CompareTo(y.Age);
default:
return x.Age.CompareTo(y.Age);
}
}
}
Return门徒 2009-12-21
  • 打赏
  • 举报
回复
你出了这么长的代码,倒是给个重点的地方啊~!眼看花了~!
houzy123 2009-12-21
  • 打赏
  • 举报
回复
排序方法是BubbleSort()
其要调用public int CompareTo(Person p) 方法有比较结果
如果是按age排序则是取age - p.age的比较值,如果是按要CompareTo(Person p)返回return score - p.score;
我对CompareTo()方法进行重载也不行

jbo126 2009-12-18
  • 打赏
  • 举报
回复
你蓝色注释的那个地方,是什么意思?

你“分别对age,score进行排序 ”怎么理解,是对二者分别进行排序,还是当age相等时,再比较score?
我姓区不姓区 2009-12-18
  • 打赏
  • 举报
回复

public int CompareTo(Person p)
{
if(this.age == p.age)
return this.score.CompareTo(p.score);
return this.age.CompareTo(p.age);
}

bancxc 2009-12-18
  • 打赏
  • 举报
回复
没看懂怎么排序

110,561

社区成员

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

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

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