Created
March 11, 2014 00:36
-
-
Save kmb385/9477347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
public class Student { | |
private String name; | |
public static Comparator<Student> getCompByName() { | |
Comparator<Student> comp = new Comparator<Student>() { | |
@Override | |
public int compare(Student s1, Student s2) { | |
return s1.name.compareTo(s2.name); | |
} | |
}; | |
return comp; | |
} | |
public Student(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public static void main(String[] args) { | |
Student[] studentArr = new Student[]{new Student("Mike"),new Student("Hector"), new Student("Reggie"),new Student("zark")}; | |
List<Student> students = new ArrayList<Student>(Arrays.asList(studentArr)); | |
Collections.sort(students, Student.getCompByName()); | |
for(Student student:students){ | |
System.out.println(student.getName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment