Skip to content

Instantly share code, notes, and snippets.

@kmb385
Created March 11, 2014 00:36
Show Gist options
  • Save kmb385/9477347 to your computer and use it in GitHub Desktop.
Save kmb385/9477347 to your computer and use it in GitHub Desktop.
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