Search code examples
javaandroidsortingpojo

Sorting objects of different types into one list


I have the following problem and I'm not quite sure how to go about solving it. I am requesting data from several different servers with each server returning a list of objects of a different type (every server has it's own specific unique POJO object type) - but what all these lists of objects have in common is that all the objects have a date parameter. I then need to display the objects from all the lists as one huge mixed list for the user, with all the various objects sorted by date.

I made an adapter that extends baseadapter and I've passed all the arraylists of objects to that adapter - but how can I sort all these lists of objects by date and display them all together? I could create a "super object" that has the properties of all the other objects and then make one sorted array of this super object to pass to the adapter, but that seems like a messy and crude solution. Is there a more or less elegant way to do this?

public class someAdapter extends BaseAdapter
{
...

public someAdapter(ArrayList<ObjectOne> objectOneArray, ArrayList<ObjectTwo> objectTwoArray) {
        if (objectOneArray != null) {
            this.localObjectOneList.addAll(objectOneArray);
        }
        ...
}
}

Example of an object type:

public class ObjectOne implements Serializable {
private String date;
private String someFieldOne;
private String someFieldTwo;

...
//getters and setters
...
}

To reiterate - the final list has to be a mix of all the different object types in the order of their dates.


Solution

  • The way you expected initially to have a super class would be nice if there weren't a huge plumbing associated. A better approach can be as follows: Assuming that all the POJOs have getDate() method.

    1. Declare a method getDate() to retrieve date object from any POJO.
    2. Declare a Comparator to compare dates
    3. Sort the objects

      If the POJOs don't have getters i.e, getDate() method, replace getDateFromMethod() with getDateFromProperty() in the comparator .

      public class Sorter{ public void sortList()
      {
          Collections.sort(objList, comparator);
      
          // Now consume / print your objList to get sorted by date
      }
      
      ArrayList<Object>   objList     = new ArrayList<Object>();
      
      Comparator<Object>  comparator  = new Comparator<Object>()
                                      {
      
      
      
                                      @Override
                                      public int compare(Object object1, Object o2)
                                          {
                                              Date o1Date = getDateFromMethod(object1);
                                              Date o2Date = getDateFromMethod(o2);
                                              if (o1Date != null && o2Date != null)
                                              {
                                                  if (o1Date.before(o2Date))
                                                      return -1;
                                                  else if (o1Date.after(o2Date))
                                                      return 1;
      
                                                  else
                                                      return 0;
                                              }
                                              else
                                                  return -1;
                                          }
                                      };
      
      public Date getDateFromMethod(Object obj)
      {
      
          Date date = null;
          String getDate = "getDate";
          try
          {
              Method method = obj.getClass().getMethod(getDate, (Class<?>[]) null);
              if (method != null)
                  date = (Date) method.invoke(obj, (Object[]) null);
          }
          catch (NoSuchMethodException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          catch (SecurityException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          catch (IllegalAccessException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          catch (IllegalArgumentException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          catch (InvocationTargetException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      
          return date;
      }public Date getDateFromProperty(Object obj)
                                  {
                                      Date date=null;
                                      try
                                      {
                                      Field dateField=    obj.getClass().getField("date");
                                      if(dateField!=null)
                                          date=(Date) dateField.get(obj);
                                      }
                                      catch (NoSuchFieldException e)
                                      {
                                          // TODO Auto-generated catch block
                                          e.printStackTrace();
                                      }
                                      catch (SecurityException e)
                                      {
                                          // TODO Auto-generated catch block
                                          e.printStackTrace();
                                      }
                                      catch (IllegalArgumentException e)
                                      {
                                          // TODO Auto-generated catch block
                                          e.printStackTrace();
                                      }
                                      catch (IllegalAccessException e)
                                      {
                                          // TODO Auto-generated catch block
                                          e.printStackTrace();
                                      }
      
                                      return date;
      
                                  }
      

      }

      Now say for instance you have Obj1,Obj2 and Obj3 as follows, create objects using these POJOS and populate the objList and test the solution.

    class Obj1 { int id; int name; Date date;

    public Date getDate()
    

    { return date; } public int getId() { return id; }

    public void setId(int id)
    {
        this.id = id;
    }
    
    public int getName()
    {
        return name;
    }
    
    public void setName(int name)
    {
        this.name = name;
    }
    

    }

    class Obj2 { int sequence; int location; Date date;

    public Date getDate()
    

    { return date; } public int getSequence() { return sequence; }

    public void setSequence(int sequence)
    {
        this.sequence = sequence;
    }
    
    public int getLocation()
    {
        return location;
    }
    
    public void setLocation(int location)
    {
        this.location = location;
    }
    

    }

    class Obj3 { int type; int origin; Date date;

    public Date getDate()
    

    { return date; }

    public void setDate(Date date) { this.date = date; }

    public int getType()
    {
        return type;
    }
    
    public void setType(int type)
    {
        this.type = type;
    }
    
    public int getOrigin()
    {
        return origin;
    }
    
    public void setOrigin(int origin)
    {
        this.origin = origin;
    }
    

    }