001package algs25; 002import stdlib.*; 003import java.util.Arrays; 004import java.util.Comparator; 005/* *********************************************************************** 006 * Compilation: javac Student.java 007 * Execution: java Student 008 * 009 * Illustrates implementation of a Comparator 010 * 011 * % By name 012 * ---------- 013 * 2 Alice 014 * 1 Bob 015 * 2 Carol 016 * 1 Dave 017 * 2 Eve 018 * 3 Frank 019 * 1 Grant 020 * 3 Helia 021 * 3 Isaac 022 * 1 Jen 023 * 1 Kevin 024 * 2 Larry 025 026 * By section 027 * ---------- 028 * 1 Bob 029 * 1 Dave 030 * 1 Grant 031 * 1 Jen 032 * 1 Kevin 033 * 2 Alice 034 * 2 Carol 035 * 2 Eve 036 * 2 Larry 037 * 3 Frank 038 * 3 Helia 039 * 3 Isaac 040 * 041 * By Kevin 042 * ---------- 043 * 1 Kevin 044 * 2 Larry 045 * 2 Alice 046 * 1 Bob 047 * 2 Carol 048 * 1 Dave 049 * 2 Eve 050 * 3 Frank 051 * 1 Grant 052 * 3 Helia 053 * 3 Isaac 054 * 1 Jen 055 * 056 *************************************************************************/ 057 058public class XStudent { 059 060 public static final Comparator<XStudent> BY_NAME = new ByName(); 061 public static final Comparator<XStudent> BY_SECTION = new BySection(); 062 public final Comparator<XStudent> BY_MY_NAME = new ByMyName(); 063 064 private final String name; 065 private final int section; 066 067 // constructor 068 public XStudent(String name, int section) { 069 this.name = name; 070 this.section = section; 071 } 072 073 // comparator to sort by name 074 private static class ByName implements Comparator<XStudent> { 075 public int compare(XStudent a, XStudent b) { 076 return a.name.compareTo(b.name); 077 } 078 } 079 080 // comparator to sort by section 081 private static class BySection implements Comparator<XStudent> { 082 public int compare(XStudent a, XStudent b) { 083 return a.section - b.section; 084 } 085 } 086 087 // comparator to sort by name with this name first 088 // illustrates the use of a non-static comparator 089 private class ByMyName implements Comparator<XStudent> { 090 public int compare(XStudent a, XStudent b) { 091 if (a.name.compareTo(b.name) == 0) return 0; 092 if (a.name.compareTo(name) == 0) return -1; 093 if (b.name.compareTo(name) == 0) return +1; 094 if ((a.name.compareTo(name) < 0) && (b.name.compareTo(name) > 0)) 095 return +1; 096 if ((a.name.compareTo(name) > 0) && (b.name.compareTo(name) < 0)) 097 return -1; 098 return a.name.compareTo(b.name); 099 } 100 } 101 102 // return string representation 103 public String toString() { 104 return section + " " + name; 105 } 106 107 108 // test client 109 public static void main(String[] args) { 110 111 // create an array of students 112 XStudent alice = new XStudent("Alice", 2); 113 XStudent bob = new XStudent("Bob", 1); 114 XStudent carol = new XStudent("Carol", 2); 115 XStudent dave = new XStudent("Dave", 1); 116 XStudent eve = new XStudent("Eve", 2); 117 XStudent frank = new XStudent("Frank", 3); 118 XStudent grant = new XStudent("Grant", 1); 119 XStudent helia = new XStudent("Helia", 3); 120 XStudent isaac = new XStudent("Isaac", 3); 121 XStudent jen = new XStudent("Jen", 1); 122 XStudent kevin = new XStudent("Kevin", 1); 123 XStudent larry = new XStudent("Larry", 2); 124 XStudent[] students = { 125 larry, kevin, jen, isaac, grant, helia, 126 frank, eve, dave, carol, bob, alice 127 }; 128 129 // sort by name and print results 130 StdOut.println("By name"); 131 StdOut.println("----------"); 132 Arrays.sort(students, BY_NAME); 133 for (XStudent student : students) 134 StdOut.println(student); 135 StdOut.println(); 136 137 138 // now, sort by section and print results 139 StdOut.println("By section"); 140 StdOut.println("----------"); 141 Arrays.sort(students, BY_SECTION); 142 for (XStudent student : students) 143 StdOut.println(student); 144 StdOut.println(); 145 146 // now, sort by name relative to Kevin 147 StdOut.println("By Kevin"); 148 StdOut.println("----------"); 149 Arrays.sort(students, kevin.BY_MY_NAME); 150 for (XStudent student : students) 151 StdOut.println(student); 152 StdOut.println(); 153 154 155 } 156 157}