Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class MyComp implements Comparator<MyObj> {
- public int compare(MyObj mo1, MyObj mo2){
- if (mo1.y < mo2.y)
- return -1;
- if (mo1.y > mo2.y)
- return 1;
- return 0;
- }
- }
- class MyAnotherComp implements Comparator<MyObj> {
- public int compare(MyObj mo1, MyObj mo2){
- if (mo1.z < mo2.z)
- return -1;
- if (mo1.z > mo2.z)
- return 1;
- return 0;
- }
- }
- class MyObj implements Comparable<MyObj> {
- public int x;
- public int y;
- public int z;
- public MyObj(int x, int y, int z){
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public int compareTo(MyObj mo){
- if (this.x < mo.x)
- return -1;
- if (this.x > mo.x)
- return 1;
- return 0;
- }
- public String toString(){
- return "[" + x + ", " + y + ", " + z + "]";
- }
- }
- public class Sandbox {
- public static void main(String args[]) {
- try {
- MyObj[] a = new MyObj[3];
- a[0] = new MyObj(1,2,3);
- a[1] = new MyObj(4,3,2);
- a[2] = new MyObj(3,1,2);
- for(int i = 0; i < a.length; ++i){
- System.out.print(a[i]);
- }
- System.out.println();
- Arrays.sort(a);
- for(int i = 0; i < a.length; ++i){
- System.out.print(a[i]);
- }
- System.out.println();
- Arrays.sort(a, new MyComp());
- for(int i = 0; i < a.length; ++i){
- System.out.print(a[i]);
- }
- System.out.println();
- Arrays.sort(a, new MyAnotherComp());
- for(int i = 0; i < a.length; ++i){
- System.out.print(a[i]);
- }
- System.out.println();
- } catch(Exception e){
- System.out.println(e);
- System.out.println("Unexpected error, sorry!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement