-1

I have two different methods which fetch data from tow different DB tables. I will add both data into same ArrayList using bean class.

List<LeadVO> leadvoList = new ArrayList<LeadVO>(); // array list declaration

In the while loop I will load all the data using bean class. Before this action I will fire query for both tables.

while(true){
     LeadVO statusVO = new LeadVO(); //initializing bean class 

// code to load all value using setters 

Finally I will add this bean to array list:

 leadvoList.add(statusVO);

created seperate class to compare

public class ComparatorDAO implements Comparator {

public int compare(LeadVO arg0, LeadVO arg1) {


    return arg1.getCreatedtimeFormat().compareTo(arg0.getCreatedtimeFormat()) ;

}

}

Collections.sort(commentVOList,new ComparatorDAO()); //sorting method

ideally this will not sort according to date i believe this will treat date as string

please help me in this

thanks once again

Now I need to sort this list in date order which is already present in the list. I mean the date which is present in the list.

Manju Kb
  • 51
  • 1
  • 8
  • 1
    Like [that](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property)? – John Oct 31 '14 at 13:49
  • @Manju I just updated your code,there might be few small syntax error since I did not test. – Lokesh Oct 31 '14 at 13:56

4 Answers4

1

If your LeadVO contains the date you want to sort by, implement Comparable interface in your VO and then sort the VO collection using Collections.sort().

public class LeadVO implements Comparable<LeadVO> {
    private Date date;

    // other properties, getters and setters

    @Override
    public int compareTo(LeadVO other) {
        return date.compareTo(other.getDate());
    }
}

and the sort like this:

Collections.sort(leadVoList);

You should ideally add null checks or use something like ObjectUtils.compare from commons-lang if your date is not guaranteed to be non-null.

Also you could do this by creating a Comparator instead of implementing Comparable as suggested by other posters, which might be better if you need to sort your VO by multiple values. If you just need to sort it by date, this approach might be a little simpler.

Bohuslav Burghardt
  • 33,626
  • 7
  • 114
  • 109
0

Here is the Example:Hope you don't mind hard interpretation.

 List<String> unsortList = new ArrayList<String>();

    unsortList.add("CCC");
    unsortList.add("111");
    unsortList.add("AAA");
    unsortList.add("BBB");
    unsortList.add("ccc");
    unsortList.add("bbb");
    unsortList.add("aaa");
    unsortList.add("333");
    unsortList.add("222");

    //before sort
    System.out.println("ArrayList is unsort");
    for(String temp: unsortList){
        System.out.println(temp);
    }

    //sort the list
    Collections.sort(unsortList);

    //after sorted
    System.out.println("ArrayList is sorted");
    for(String temp: unsortList){
        System.out.println(temp);
    }
}
Lokesh
  • 313
  • 2
  • 12
0

You can sort arrayList using a comparator:

public Comparator<Record> RecordComparator = new Comparator<Record>() {
    public int compare(Record record1, Record record2) {
        return record1.getDate().compareTo(record2.getDate());
    }
};

and then:

Collections.sort(list, new RecordComparator());
jjlema
  • 850
  • 5
  • 8
0

U'll need to override that class compareTo method as it is the method which java looks at for sorting

Simply override the fields that u need sorted

compareTo(Object other){Someclass.somefield.compareTo(other.smeField)}
MRK187
  • 1,545
  • 2
  • 13
  • 20