A simple question, but I'm confused =) I need to sort the objects, I redefine compareTo, and everything works, but I need to select different criteria for sorting these objects.

  • came up with an option, but I would like to hear more ... - Gorets

2 answers 2

Perhaps this will help you somehow:

import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Testing { public static void main(String[] argv) { List<Ololo> list = new ArrayList<Ololo>(); list.add(new Ololo("Chuck",71)); list.add(new Ololo("Ivan", 27)); list.add(new Ololo("Pavel",41)); list.add(new Ololo("Dmitriy",36)); for(int i=0;i<list.size();i++) { System.out.println(list.get(i).getName()+": "+list.get(i).getAge()); } System.out.println("\n"); Collections.sort(list,new Ololo.SortByName()); for(int i=0;i<list.size();i++) { System.out.println(list.get(i).getName()+": "+list.get(i).getAge()); } System.out.println("\n"); Collections.sort(list,new Ololo.SortByAge()); for(int i=0;i<list.size();i++) { System.out.println(list.get(i).getName()+": "+list.get(i).getAge()); } } } class Ololo { private String name = null; private int age = 0; public Ololo(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public static class SortByName implements Comparator<Ololo> { @Override public int compare(Ololo t, Ololo t1) { return (int) t.name.compareTo(t1.name); } } public static class SortByAge implements Comparator<Ololo> { @Override public int compare(Ololo t, Ololo t1) { return t.age - t1.age; } } } 
  • @gorets, There are quite a few options with the comparator, as you see. The method of including the IMHO sort flags in the class cannot be considered good, because You will need to change it in all elements of the collection if you decide to re-sort it. At first glance, removing a flag to a static element of a class solves a problem, but a problem will arise when parallel sorting of different collections (arrays) of elements of a given class. - avp
  • 1) wrong class names should be: NameComparator, AgeComparator 2) they will be more flexible if you add a boolean reverse parameter to the class constructor. which allows you to switch the sorting mode: direct / reverse ps class name Ololo itself is “self-documenting” and “adequate” - jmu

View the documentation. For example, here is one of the methods for java.util.Arrays

 sort(T[] a, Comparator<? super T> c) 
  • Well, it's clear, I have to expand the Comparator, for each type of sorting and pass it to the method, not bad at all, but it turns out that I need to make many small sorting classes, I thought up to put a flag in the element - sorting type, and in the compareTo method, check it select desired comparison criteria - Gorets
  • 2
    I usually call sort just with a new comparator text, something like: Arrays.sort (arr, new Comparator <MyClass> () {@Override public int compare (MyClass arg0, MyClass arg1) {// TODO Auto-generated method stub return arg0.age-arg1.age;}}); - avp