1

I want to do sorting of data on click of a button.

I am parsing a XML and storing its value in a List with the help of this class

  public class MyTripRespone {
    private List<MyTripCorporateBookingApprovals> coroprateBookingDetails = null;

    public List<MyTripCorporateBookingApprovals> getCoroprateBookingDetails() {
        return coroprateBookingDetails;
    }

    public void setCoroprateBookingDetails(
            List<MyTripCorporateBookingApprovals> coroprateBookingDetails) {
        this.coroprateBookingDetails = coroprateBookingDetails;
    }

}

In the Approved Class i have 3 values that are date.id,name.

 public class MyTripCorporateBookingApprovals {

    private String insertTime ="";
    private String txid ="";
    private String journeyDate = "";
    private String passengerFirstName = "";


    public String getInsertTime() {
        return insertTime;
    }
    public void setInsertTime(String insertTime) {
        this.insertTime = insertTime;
    }
    public String getTxid() {
        return txid;
    }
    public void setTxid(String txid) {
        this.txid = txid;
    }

    public String getJourneyDate() {
        return journeyDate;
    }
    public void setJourneyDate(String journeyDate) {
        this.journeyDate = journeyDate;
    }
    public String getReturnDate() {
        return returnDate;
    }
    public void setReturnDate(String returnDate) {
        this.returnDate = returnDate;
    }
    public String getPassengerTitle() {
        return passengerTitle;
    }
    public void setPassengerTitle(String passengerTitle) {
        this.passengerTitle = passengerTitle;
    }
    public String getPassengerFirstName() {
        return passengerFirstName;
    }
    public void setPassengerFirstName(String passengerFirstName) {
        this.passengerFirstName = passengerFirstName;
    }




}

Likewise for name and id from this I am putting the values in the layout that I am inflating dynamically in a loop. So I want when the user clicks on sortByDate. The values are arranged by date in a decreasing order. Means latest date first and then so on. How can we do this? Please help .

After kalyan suggestion

public class MyTripComparator implements Comparator<MyTripCorporateBookingApprovals>{

    @Override
    public int compare(MyTripCorporateBookingApprovals lhs,
            MyTripCorporateBookingApprovals rhs) {
        return lhs.getJourneyDate().compareTo(rhs.getJourneyDate());
    }



}

public void sortByDate(View v) {
    Collections.sort(//what i have to here, new MyTripComparator());

    }
  • The above code is good..Collections.sort(//what i have to here, new MyTripComparator()); but here as a first arguement you have to pass which list of objects you want to sort..In this class MyTripRespone you are setting List coroprateBookingDetails you have to pass that list object.. – kalyan pvs Oct 09 '13 at 06:28
  • List coroprateBookingDetails = null; Collections.sort(coroprateBookingDetails, new MyTripComparator());like this or some other way –  Oct 09 '13 at 06:32
  • if coroprateBookingDetails is not null and contains all your objects that is fine.. – kalyan pvs Oct 09 '13 at 06:33
  • but i have to pass setApprovalDetailsData(MyTripRespone data) how i will get that from this –  Oct 09 '13 at 06:34
  • from which i am infalting the layout and seeting the values .it needs MyTripResponse –  Oct 09 '13 at 06:35
  • Collections.sort() will retuns the sorted list then set to that.. – kalyan pvs Oct 09 '13 at 06:36
  • MyTripRespone data= Collections.sort(coroprateBookingDetails, new MyTripComparator()); //Error is Type mismatch: cannot convert from void to MyTripRespone –  Oct 09 '13 at 06:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38842/discussion-between-rahul-and-kalyan-pvs) –  Oct 09 '13 at 06:38

1 Answers1

0

Try this

In MyTripCorporateBookingApprovals Class

public class MyTripCorporateBookingApprovals implements
        Comparator<MyTripCorporateBookingApprovals>,
        Comparable<MyTripCorporateBookingApprovals> {

        }

  @Override
    public int compare(MyTripCorporateBookingApprovals lhs,
            MyTripCorporateBookingApprovals rhs) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(lhs.getJourneyDate())
                    .compareTo(
                            new SimpleDateFormat("yyyy-MM-dd").parse(rhs
                                    .getJourneyDate()));
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }

@Override
    public int compareTo(MyTripCorporateBookingApprovals another) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd")
                    .parse(this.getJourneyDate()).compareTo(
                            new SimpleDateFormat("yyyy-MM-dd").parse(another
                                    .getJourneyDate()));
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }

in main Class from where you have to use this

Collections.sort(tripParseData.getCoroprateBookingDetails());
Developer
  • 6,292
  • 19
  • 55
  • 115