intermediate/ComparatorExample

From ggc

Jump to: navigation, search

01 package intermediate;
02 
03 import wiki.Wiki;
04 import java.awt.*;
05 import java.util.*;
06 
07 /**
08  * All about my application here.
09  @author Jam Jenkins
10  */
11 public class ComparatorExample
12 {
13   static class CompareByX implements Comparator<Point>
14   {
15     public int compare(Point left, Point right)
16     {
17       return left.x-right.x;
18     }
19 
20     public boolean equal(Point left, Point right)
21     {
22       return left.x==right.x;
23     }
24   }
25 
26   static class CompareByY implements Comparator<Point>
27   {
28     public int compare(Point left, Point right)
29     {
30       return left.y-right.y;
31     }
32 
33     public boolean equal(Point left, Point right)
34     {
35       return left.y==right.y;
36     }
37   }
38   public static void main(String[] args)
39   {
40     Point[] points={
41                        new Point(45),
42                        new Point(38),
43                        new Point(52),
44                        new Point(110)
45                    };
46     Arrays.sort(points, new CompareByX());
47     Wiki.out.println("Sorted by x:");
48     for(Point p: points)
49       Wiki.out.println(p);
50 
51     Arrays.sort(points, new CompareByY());
52     Wiki.out.println("Sorted by y:");
53     for(Point p: points)
54       Wiki.out.println(p);
55 
56     Wiki.out.println("Add your code here.");
57   }
58 }


Download/View intermediate/ComparatorExample.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter