user1413969
user1413969

Reputation: 1291

Having multiple types of sorts for an object

Lets say that I have an java object with multiple String fields.

public class Person {
    private String field1
    // keeps going 

I want to be able to have the ability to sort a list of persons depending on whatever field I choose.

I know that I can use the comparator interface and implement multiple compareTo's as described in the topic: How do I make 2 comparable methods in only one class?

But while using Collections.sort()

But is there some way for me to do this without all this repeating code?

Upvotes: 3

Views: 165

Answers (4)

perencia
perencia

Reputation: 1542

This is an sketch of a generic approach.

It should work as long as the types of the fields implement the compareTo method (that is, you should use Integer vs int and so on)

DISCLAIMER: I have not taken into account any performance issues :)

Sorter.java

package sovflow;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Sorter<ObjectType, FieldType extends Comparable<FieldType>> {

    public List<Wrapper<ObjectType, FieldType>> sort(List<ObjectType> list,
            Field field) {
        field.setAccessible(true);
        List<Wrapper<ObjectType, FieldType>> _list = new ArrayList<Wrapper<ObjectType, FieldType>>();
        for (ObjectType object : list) {
            Wrapper<ObjectType, FieldType> wrapper = new Wrapper<ObjectType, FieldType>(
                    field, object);
            _list.add(wrapper);
        }
        Collections.sort(_list);
        return _list;
    }

}

Wrapper.java

package sovflow;

import java.lang.reflect.Field;

public class Wrapper<ObjectType, FieldType extends Comparable<FieldType>>
        implements Comparable<Wrapper<ObjectType, FieldType>> {

    Field field;
    ObjectType object;

    public Wrapper(Field field, ObjectType object) {
        this.field = field;
        this.object = object;
    }

    @SuppressWarnings("unchecked") // for the field.get. Unavoidable, i'd say :)
    public int compareTo(Wrapper<ObjectType, FieldType> paramT) {
        try {   
            return ((FieldType) field.get(object)).compareTo((FieldType)field.get(paramT.object));
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return 0;
    }

    public void print() {
        System.out.println(object);

    }

}

This is an example of how to use it:

package sovflow;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Main {
    private static class Person {

        private Integer id;
        private String name;
        public Person(Integer id, String name) {
            super();
            this.id = id;
            this.name = name;
        }
        @Override
        public String toString() {
            return "Id = " + id + " name = " + name;
        }       
    }

    public static void main(String[] args) {
        Person p = new Person(1, "B");
        Person p2 = new Person(2, "A");
        List<Person> list = new ArrayList<Person>();
        list.add(p);
        list.add(p2);
        Sorter<Person, String> sorter = new Sorter<Person, String>();
        try {
            Field declaredField = Person.class.getDeclaredField("name");
            // Just to show the result. The sort method could be modified to
            // not having to deal with the wrapper here..
            List<Wrapper<Person,String>> list2 = sorter.sort(list, declaredField);          
            for (Wrapper<Person, String> wrapper : list2) {
                wrapper.print();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

Upvotes: 1

Simon Dorociak
Simon Dorociak

Reputation: 33505

I want to be able to have the ability to sort a list of persons depending on whatever field I choose.

@JB Nizet's solution for Java 8 will make a trick but it works only from Java 8. What about version(s) lower than 8 which are used by majority of people?

I will provide you i think simple solution for lower versions than Java 8.

You really don't need to create more than one Comparator class (you mentioned it as "repeating code"). What about to use proper techniques like if conditions or enums?

private Comparator<YourClass> getComparator(MyPropertiesEnum myEnum) {
   return new Comparator<YourClass>() {

      @Override
      public int compare(YourClass o1, YourClass o2) { {
         switch (myEnum) {
            case FIRSTNAME:
               // implementation for firstname
            break;
            case LASTNAME:
              // implementation for lastname
            break;
            default:
               // implementation for default version
            break;           
         }
      }
   };
}

Where MyPropertiesEnum can looks like:

public enum MyPropertiesEnum {
   FIRSTNAME, LASTNAME, AGE;
}

Note: Also you can use proper if conditions if they are more comfortable for you.

Hope it'll help you solve your issue.

Upvotes: 3

ajb
ajb

Reputation: 31689

public class Person {
    private String field1;
    private String field2;
    ...
    private Map<String,String> allFields;

    ...
    public void setField1 (String value) {
        field1 = value;
        allFields.put("field1",value);
    }

    public void setField2 (String value) {
        field2 = value;
        allFields.put("field2",value);
    }

Or whatever names you want. Or you can use an enum instead of a String as the map key. This demonstrates why using getters and setters is a good idea, by the way; you couldn't do this by using public member fields and letting clients assign to them directly. But since you make the fields private and require a setter to set them, you can ensure that the String field and the map entry always match.

Now it should be easy to write a single Comparator that looks for the desired field by looking it up in allFields.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691715

If you don't like the verbosity of Comparator classes, then use Java 8 and its lambda expressions:

Comparator<Person> byName = Comparator.comparing(Person::getName);
Comparator<Person> byBirthDate = Comparator.comparing(Person::getBirthDate);

Upvotes: 8

Related Questions