@Test public void sortByExample4() { List<Integer> example = asList(1, 3, 5, 2, 4, 6); final List<String> expected = asList("1", "3", "5", "2", "4", "6"); assertEquals( expected, Algorithms.sortByExample( example, Strings.<Integer>string(), asList("6", "5", "4", "3", "2", "1"), Functions.<String>identity())); assertEquals( asList("3", "5", "4"), Algorithms.sortByExample( example, Strings.<Integer>string(), asList("5", "4", "3"), Functions.<String>identity())); assertEquals( expected, Algorithms.sortByExample( example, Strings.<Integer>string(), asList("1", "2", "3", "4", "5", "6", "7", "8", "9"), Functions.<String>identity())); }
@Test public void sortByExample1() { List<Integer> example = asList(1, 3, 5, 2, 4, 6); assertEquals(example, Algorithms.sortByExample(example, asList(6, 5, 4, 3, 2, 1))); assertEquals(asList(3, 5, 4), Algorithms.sortByExample(example, asList(5, 4, 3))); assertEquals(example, Algorithms.sortByExample(example, asList(1, 2, 3, 4, 5, 6, 7, 8, 9))); }
@Test public void sortByExample3() { // We have the names of some people in the right order, but then we want to sort the actual // Person objects // into the same order. Providing a mapping function from the Person to his/her name is all we // need. final Person huey = new Person("huey"); final Person dewey = new Person("dewey"); final Person louie = new Person("louie"); List<String> example = asList("donald", huey.getName(), dewey.getName(), louie.getName()); assertEquals( asList(huey, dewey, louie), Algorithms.sortByExample( example, asList(dewey, louie, huey), new Function<Person, String>() { public String apply(Person p) { return p.getName(); } })); }