51,396
社区成员




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 + "|");
}
}
}
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]+"|");
}
}
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());
}