一个排序问题。求解

云中雁荡山 2011-07-14 10:09:40
有如下几个学生。学生信息包括,姓名,年龄,成绩。
A:ccp12,20,75
B:bpp12,20,75
C:Adp12,20,73
D:Jack1,18,70
E:Appl2,20,70


依次按照成绩、姓名、年龄升序排列。

用java语言实现。

题目给出的答案是:Appl2(20):70|Jack1(18):70|Adp12(20):73|bpp12(20):75|ccp12(20):75

请高手指点,谢谢
...全文
142 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunminshiwo 2011-07-17
  • 打赏
  • 举报
回复
都解决了啊。。。
我习惯用的Comparator,因为经常遇到需要比较的东西不是我的,我不好修改其源代码的情况


import java.util.Arrays;
import java.util.Comparator;

class OuchiExplorerCompatator implements Comparator
{
@Override
public int compare(Object o1, Object o2)
{
OuchiExplorer e1 = (OuchiExplorer)o1;
OuchiExplorer e2 = (OuchiExplorer)o2;
if (e1.score != e2.score)
{
return e1.score - e2.score;
}
else
{
if (!e1.name.equals(e2.name))
{
return e1.name.compareTo(e2.name);
}
else
{
return e1.age - e2.age;
}
}
}
}

public class OuchiExplorer
{
protected String name;
protected int age;
protected int score;

public OuchiExplorer(String name, int age, int score)
{
this.name = name;
this.age = age;
this.score = score;
}

@Override
public String toString()
{
return String.format("%s(%d):%d", name, age, score);
}

public static void main(String args[])
{
OuchiExplorer[] ors = new OuchiExplorer[5];
ors[0] = new OuchiExplorer("ccp12", 20, 75);
ors[1] = new OuchiExplorer("bpp12", 20, 75);
ors[2] = new OuchiExplorer("Adp12", 20, 73);
ors[3] = new OuchiExplorer("Jack1", 18, 70);
ors[4] = new OuchiExplorer("Appl2", 20, 70);

Arrays.sort(ors, new OuchiExplorerCompatator());

for (OuchiExplorer or : ors)
{
System.out.print(or + "|");
}
}
}

云中雁荡山 2011-07-16
  • 打赏
  • 举报
回复
楼上的MM,已经知道怎么写了,问题已经解决。谢谢你们的关注。有时间我会把相关东西做一下总结发出来的。
飞跃颠峰 2011-07-15
  • 打赏
  • 举报
回复
多属性的排序器要合写在一个方法中


import java.util.Arrays;

public class Student implements Comparable<Student> {
protected String name;
protected int age;
protected int score;

public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}

// 排序器
public int compareTo(Student stu) {
if (score!=stu.score)
return score-stu.score;
else {
int c = name.compareTo(stu.name);
return c!=0 ? c : age - stu.age;
}
}

public String toString() {
return name + "(" + age + "):" + score;
}

public static void main(String[] args) {

//初始化数据
Student[] sts = new Student[5];
sts[0] = new Student("ccp12", 20, 75);
sts[1] = new Student("bpp12", 20, 75);
sts[2] = new Student("Adp12", 20, 73);
sts[3] = new Student("Jack1", 18, 70);
sts[4] = new Student("Appl2", 20, 70);

Arrays.sort(sts); //排序

//输出
for (int i=0; i<sts.length; i++)
System.out.print(sts[i]+"|");
}
}
云中雁荡山 2011-07-14
  • 打赏
  • 举报
回复
有兴趣的朋友可以一起探讨以下多属性排序器的写法。
gentleboy2009 2011-07-14
  • 打赏
  • 举报
回复
参考这个吧:http://geshenyi.iteye.com/blog/939914
云中雁荡山 2011-07-14
  • 打赏
  • 举报
回复
就是不知道排序规则出了啥问题,才发出来问的。我总共定义了三个排序规则。这里我发出来两个吧。
以下为成绩的排序规则:

public int compare(Object o1, Object o2)
{
Student p1 = (Student) o1;
Student p2 = (Student) o2;
if (p1.getGrade() < p2.getGrade())
{
return 1;
}
else
{
return 0;
}
}



以下为姓名的排序规则

public int compare(Object o1, Object o2)
{
Student p1 = (Student) o1;
Student p2 = (Student) o2;
return p1.getName().compareTo(p2.getName());
}
gentleboy2009 2011-07-14
  • 打赏
  • 举报
回复
你定义的排序规则有问题?
云中雁荡山 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 gentleboy2009 的回复:]

实现Comparator接口,也就是定义排序规则。这样可以实现你的要求~
[/Quote]

你说的这个我做过了,得出的结果和答案不相同
gentleboy2009 2011-07-14
  • 打赏
  • 举报
回复
实现Comparator接口,也就是定义排序规则。这样可以实现你的要求~

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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