final List persons = Arrays.asList(BeanSorterTest.persons);
// - try simple property sorting
List sorted = BeanSorter.sort(
persons,
new SortCriteria[] {new SortCriteria(
"firstName",
Ordering.ASCENDING)});
Iterator iterator = sorted.iterator();
Person billy = (Person)iterator.next();
assertEquals(billy.getFirstName(), "Billy");
Person chad = (Person)iterator.next();
assertEquals(chad.getFirstName(), "Chad");
Person john = (Person)iterator.next();
assertEquals(john.getFirstName(), "John");
sorted = BeanSorter.sort(
persons,
new SortCriteria[] {new SortCriteria(
"firstName",
Ordering.DESCENDING)});
iterator = sorted.iterator();
john = (Person)iterator.next();
assertEquals(john.getFirstName(), "John");
chad = (Person)iterator.next();
assertEquals(chad.getFirstName(), "Chad");
billy = (Person)iterator.next();
assertEquals(billy.getFirstName(), "Billy");
// - try nested property sorting
sorted = BeanSorter.sort(
persons,
new SortCriteria[] {new SortCriteria(
"address.streetNumber",
Ordering.ASCENDING)});
iterator = sorted.iterator();
john = (Person)iterator.next();
assertEquals(john.getFirstName(), "John");
chad = (Person)iterator.next();
assertEquals(chad.getFirstName(), "Chad");
billy = (Person)iterator.next();
assertEquals(billy.getFirstName(), "Billy");
sorted = BeanSorter.sort(
persons,
new SortCriteria[] {new SortCriteria(
"address.streetNumber",
Ordering.DESCENDING)});
iterator = sorted.iterator();
billy = (Person)iterator.next();
assertEquals(billy.getFirstName(), "Billy");