new ReverseComparator<Person>(new PersonComparator())
/** * Create a person set filter using a {@link PersonComparator} */ public PersonSetFilter() { this(new PersonComparator()); }
public void order(List<Person> persons) { PersonComparator pc = new PersonComparator() Collections.sort(persons,pc); }
return searchResults.stream() .filter((PersonSearchResult p) -> "CRDSCNCTP".equalsIgnoreCase(p.getPerson().getPersonId().getSrcCode())) .sorted(new PersonComparator()) .collect(Collectors.toList());
Person[] personArray = buildArraySomehow(); PersonComparator pc = new PersonComparator(); Arrays.sort(personArray, pc);
Collections.sort(persons, new PersonComparator());
List<Person> getListWithoutDups(List<Person> list) { TreeSet<Person> set = new TreeSet<Person>(new PersonComparator()); // use custom Comparator here Person person; // for every Person in the list for(int i = 0; i < list.size(); i++) { person = list.get(i); // if the person is already in the set (meaning it is a duplicate) // remove it from the list if(set.contains(person) { list.remove(i); i--; // make sure to accommodate for the list shifting after removal } // otherwise add it to the set of non-duplicates else { set.add(person); } } return list; }
List<Person> getListWithoutDups(List<Person> list) { List<Person> newList = new ArrayList<Person>(); TreeSet<Person> set = new TreeSet<Person>(new PersonComparator()); // use custom Comparator here // foreach Person in the list for(Person person : list) { // if the person isn't already in the set (meaning it's not a duplicate) // add it to the set and the new list if(!set.contains(person)) { set.add(person); newList.add(person); } // otherwise it's a duplicate so we don't do anything } return newList; }
PersonComparator nameComparator = new PersonComparator(); Person person = new Person("Ana"); //the age doesn't matter Person expectedPerson = EasyMock.cmp(comparisonPerson, nameComparator, LogicalOperator.EQUAL); EasyMock.expect(personService.authenticate(expectedPerson)).andReturn(true);
PersonPredicate predicate = new PersonPredicate("CRDSCNCTP"); List<PersonSearchResult> searchResults = ...; List<PersonSearchResult> filteredResult = filter(searchResult, predicate); List<PersonSearchResult> result = Collections.sort(filteredResult , new PersonComparator());
public static void main(String[] args) { PersonVO person1 = new PersonVO("firstname1", "lastname1", 2.1); PersonVO person2 = new PersonVO("firstname2", "lastname2", 1.1); List<PersonVO> personList = new ArrayList<PersonVO>(); personList.add(person1); personList.add(person2); System.out.println("Before Sort"); System.out.println(personList); Collections.sort(personList, new PersonComparator()); System.out.println("After Sort"); System.out.println(personList); }