Good day!
I have an object student with the following attributes:
class Student
String name
Date birthday
I used arrayList
Hi this is a sample that can help you to understand
package de.vogella.algorithms.sort.standardjava;
import java.util.Comparator;
public class MyIntComparable implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
}
}
package de.vogella.algorithms.sort.standardjava;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Simple2 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(4);
list.add(3);
list.add(7);
list.add(2);
list.add(1);
Collections.sort(list, new MyIntComparable());
for (Integer integer : list) {
System.out.println(integer);
}
}
}
In Java 8 you can sort the list with a one-liner using Lambda expressions and Comparator.comparing:
Collections.sort(studentList, Comparator.comparing(s -> s.getBirthday()));
Alternatively you can use the method reference:
Collections.sort(studentList, Comparator.comparing(Student::getBirthday));
This can be a tricky interview question :)
The best and reusable way i've found to solve a similar problem was to implement the interface Comparator and also creating custom Comparator according to my needs, which can be reused.
I leave here an example how to sort an ArrayList of Person according to their name attribute and also according to their gender attribute (which don't have any lexicography natural order).
The trick was defining a enum class with the custom attribute from which i wanted to sort. Applying a custom comparator to that enum attribute, the compareTo() method apply the order according to the natural order in which the values are declared (in this example male, female, others).
import java.util.*;
public class Person {
public enum Gender {
male,female, others
}
String name;
int age;
Gender gender;
public Person(String name, int age, Gender gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public Gender getGender() {
return gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
public static void printPersons(List<Person> personList) {
for (int i = 0; i < personList.size(); i++) {
System.out.println(personList.get(i).toString());
}
}
public static void printSortedByAgePersons(List<Person> personList) {
Collections.sort(personList, Person.COMPARE_BY_NAME);
for (int i = 0; i < personList.size(); i++) {
System.out.println(personList.get(i).toString());
}
}
public static void printSortedByGender(List<Person> personList) {
Collections.sort(personList, Person.COMPARE_BY_GENDER);
printPersons(personList);
}
public static Comparator<Person> COMPARE_BY_NAME = new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getName().compareTo(o2.getName());
}
};
public static Comparator<Person> COMPARE_BY_GENDER = new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getGender().compareTo(o2.getGender());
}
};
// lets test this :
public static void main(String args[]) {
Person p1 = new Person("André", 22, Gender.male);
Person p2 = new Person("Minder", 19, Gender.others);
Person p4 = new Person("Maria", 19, Gender.female);
Person p3 = new Person("Pedro", 25, Gender.male);
List<Person> personList = new ArrayList<Person>();
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);
System.out.println("original list:");
printPersons(personList);
System.out.println("------------------------------------------");
System.out.println("sorted list by name in alphabetical order");
printSortedByAgePersons(personList);
System.out.println("------------------------------------------");
System.out.println("sorted list by custom order(gender)");
printSortedByGender(personList);
}
}
You can pass a Comparator to Collections.sort() to handle the sorting by birthday:
Collections.sort(studentList, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getBirthday().compareTo(s2.getBirthday());
}
});
You'll need to add getBirthday()
to your Student
class if you don't have it already.
Here is tutorial from java to learn more about it. http://download.oracle.com/javase/tutorial/collections/interfaces/order.html
You need to write a custom comparator.
Something like:
Collections.sort(studentList, new Comparator<Student>() {
public int compare(Strudent a, Strudent b) {
return a.birthday.compareTo(b.birthday);
}
});