Mark Logan Daniel Jared Nathan Austin Trevor Seth

Qin Judy Halie Adam Gabriela James Brennan Jacquelyn 

Nick Yiming Jingzhe Paul Morgan Alex Ong Lauren Walter 

William Jack Mohan Grant M Alexander Jon 

public class Student extends Object implements Comparable<Student> {
  public int compareTo(Student other) {
    if (this.gpa > other.gpa) return -1;
    else if (this.gpa < other.gpa) return 1;
    else if (this.age < other.age) return -1;
    else if (this.age > other.age) return 1;
    else return this.name.compareTo( other.name );
  }
  String name;
  int age;
  double gpa;
  public Student(String name, int age, double gpa) {
    this.name = name;
    this.age = age;
    this.gpa = gpa;
  }
  public String toString() {
    return "Student(" + this.name + ": " + this.gpa + ", " + this.age + ")";
  }
}

import java.util.*;

class Example {
  public static void main(String[] args) {
    ArrayList<Student> students = new ArrayList<Student>();
    students.add(new Student("Tom"    , 19, 2.3));
    students.add(new Student("Chris"  , 17, 2.9));
    students.add(new Student("Leslie" , 15, 2.9));
    students.add(new Student("Dave"   ,  8, 2.9));
    students.add(new Student("Laura"  , 21, 3.7));
    students.add(new Student("Tim"    , 18, 1.6));
    students.add(new Student("Timothy", 15, 1.6));
    students.add(new Student("Adrian" , 11, 1.6));
    students.add(new Student("Aaron"  , 11, 1.6));
    students.add(new Student("Gennis" ,  9, 0.9));
    students.add(new Student("Al"     ,  5, 0.9));
    students.add(new Student("George" , 13, 3.1));

    System.out.println( students );

    Collections.sort( students );

    System.out.println( students );

  }
}

-bash-4.1$ pwd
/u/dgerman/c212-exercises/lab
-bash-4.1$ ls -l
total 8
-rw-r--r-- 1 dgerman faculty 868 Jul 21 14:36 Example.java
-rw-r--r-- 1 dgerman faculty 613 Jul 21 14:36 Student.java
-bash-4.1$ javac Example.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.1$ ls -l
total 16
-rw-r--r-- 1 dgerman faculty 1076 Jul 21 14:37 Example.class
-rw-r--r-- 1 dgerman faculty  868 Jul 21 14:36 Example.java
-rw-r--r-- 1 dgerman faculty 1115 Jul 21 14:37 Student.class
-rw-r--r-- 1 dgerman faculty  613 Jul 21 14:36 Student.java
-bash-4.1$ java Example
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[Student(Tom: 2.3, 19), Student(Chris: 2.9, 17), Student(Leslie: 2.9, 15), Student(Dave: 2.9, 8), Student(Laura: 3.7, 21), Student(Tim: 1.6, 18), Student(Timothy: 1.6, 15), Student(Adrian: 1.6, 11), Student(Aaron: 1.6, 11), Student(Gennis: 0.9, 9), Student(Al: 0.9, 5), Student(George: 3.1, 13)]
[Student(Laura: 3.7, 21), Student(George: 3.1, 13), Student(Dave: 2.9, 8), Student(Leslie: 2.9, 15), Student(Chris: 2.9, 17), Student(Tom: 2.3, 19), Student(Aaron: 1.6, 11), Student(Adrian: 1.6, 11), Student(Timothy: 1.6, 15), Student(Tim: 1.6, 18), Student(Al: 0.9, 5), Student(Gennis: 0.9, 9)]
-bash-4.1$

New problem: without modifying class Student sort the array by name lexicographically. 

Solution: define, create and pass a Comparator when invoking sort. 

import java.util.*;

// https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

public class Judge implements Comparator<Student> {
  public int compare(Student a, Student b) {
    if (a.name.compareTo(b.name) < 0) return -1; 
    else if (a.name.compareTo(b.name) > 0) return 1; 
    return 0; 
  } 
}

The class above in Judge.java and the code below requests a second sorting. 

-bash-4.1$ cat Example.java
import java.util.*;

class Example {
  public static void main(String[] args) {
    ArrayList<Student> students = new ArrayList<Student>();
    students.add(new Student("Tom"    , 19, 2.3));
    students.add(new Student("Chris"  , 17, 2.9));
    students.add(new Student("Leslie" , 15, 2.9));
    students.add(new Student("Dave"   ,  8, 2.9));
    students.add(new Student("Laura"  , 21, 3.7));
    students.add(new Student("Tim"    , 18, 1.6));
    students.add(new Student("Timothy", 15, 1.6));
    students.add(new Student("Adrian" , 11, 1.6));
    students.add(new Student("Aaron"  , 11, 1.6));
    students.add(new Student("Gennis" ,  9, 0.9));
    students.add(new Student("Al"     ,  5, 0.9));
    students.add(new Student("George" , 13, 3.1));

    System.out.println( students );

    Collections.sort( students );

    System.out.println( students );

    Collections.sort( students, new Judge() );

    System.out.println( students );

  }
}
-bash-4.1$

Here's the result of compiling and running the code:

-bash-4.1$ java Example
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[Student(Tom: 2.3, 19), Student(Chris: 2.9, 17), Student(Leslie: 2.9, 15), Student(Dave: 2.9, 8), Student(Laura: 3.7, 21), Student(Tim: 1.6, 18), Student(Timothy: 1.6, 15), Student(Adrian: 1.6, 11), Student(Aaron: 1.6, 11), Student(Gennis: 0.9, 9), Student(Al: 0.9, 5), Student(George: 3.1, 13)]
[Student(Laura: 3.7, 21), Student(George: 3.1, 13), Student(Dave: 2.9, 8), Student(Leslie: 2.9, 15), Student(Chris: 2.9, 17), Student(Tom: 2.3, 19), Student(Aaron: 1.6, 11), Student(Adrian: 1.6, 11), Student(Timothy: 1.6, 15), Student(Tim: 1.6, 18), Student(Al: 0.9, 5), Student(Gennis: 0.9, 9)]
[Student(Aaron: 1.6, 11), Student(Adrian: 1.6, 11), Student(Al: 0.9, 5), Student(Chris: 2.9, 17), Student(Dave: 2.9, 8), Student(Gennis: 0.9, 9), Student(George: 3.1, 13), Student(Laura: 3.7, 21), Student(Leslie: 2.9, 15), Student(Tim: 1.6, 18), Student(Timothy: 1.6, 15), Student(Tom: 2.3, 19)]
-bash-4.1$

--