Java 结构体排序两种方法(Comparable, Comparator)
http://blog.csdn.net/liang5630/article/details/8221041
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
Node a[];
void run() {
a = new Node[10];
for (int i = 0; i < 10; i++) {
a[i] = new Node();
a[i].x = 9 - i;
a[i].y = i;
}
Arrays.sort(a);
// Arrays.sort(a, new cmp());
// Arrays.sort(a, 0, n, new cmp());
for (int i = 0; i < 10; i++) {
System.out.println(a[i].x + " " + a[i].y);
}
}
public static void main(String[] args) {
Main solved = new Main();
solved.run();
}
// static InputStream inputStream = System.in;
// static InputReader cin = new InputReader(inputStream);
Scanner cin = new Scanner(new BufferedInputStream(System.in));
}
// Comparable< >
class Node implements Comparable<Node> {
int x, y;
public int compareTo(Node b) {
if (this.x == b.x)
return this.y - b.y;
return this.x - b.x;
}
}
// Comparator< >
class cmp implements Comparator<Node> {
public int compare(Node a, Node b) {
if (a.x == b.x) return a.y - b.y;
return a.x - b.x;
}
}