Question

I am working in an android application I want to sort a List of Objects with an Object Property. I have sorted it successfully but when I sort it all the List with that object changes the value to same as the sorted value

Please look into ma code :

SortedSet<Caseload> removeDuplicateClientName = new TreeSet<Caseload>(
            new Comparator<Caseload>() {
                @Override
                public int compare(Caseload caseload0, Caseload caseload1) {
                    return caseload0.ClientName.compareTo(caseload1.ClientName);
                }
            });

// Getting the list of values from web service
    mLISTCaseloadsHeads = parsedXML.getCaseLoadValues("get_Caseload_ClientServiceGroupID", param);
    List<Caseload> newBackUp=mLISTCaseloadsHeads ;
                    Iterator<Caseload> iterator = mCaseloadsHeads.iterator();
                    while (iterator.hasNext()) {
                        removeDuplicateClientName.add(iterator.next());
                    }
                    mCaseloadsHeads.clear();
                    mCaseloadsHeads.addAll(removeDuplicateClientName);

The List newBackUp also changes the value to the same as sorted List

Caseload class:

 public class Caseload implements Comparable<Caseload> {

        public int BusClientLogID;
        public int ClientID;
        public int ClientStatus;
        public int ClientServiceGroup_ClientSiteTherapyID;
        public String ClientName;
        public String TimeArrive;
        public String TimeDepart;
        public String SignOutTime;
        public String SignInTime;
        public String ServiceCompletedCount;
        public Boolean ShowFooter = false;

        public int getBusClientLogID() {
            return BusClientLogID;
        }

        public void setBusClientLogID(int busClientLogID) {
            BusClientLogID = busClientLogID;
        }

        public int getClientID() {
            return ClientID;
        }

        public void setClientID(int clientID) {
            ClientID = clientID;
        }

        public int getClientStatus() {
            return ClientStatus;
        }

        public void setClientStatus(int clientStatus) {
            ClientStatus = clientStatus;
        }

        public int getClientServiceGroup_ClientSiteTherapyID() {
            return ClientServiceGroup_ClientSiteTherapyID;
        }

        public void setClientServiceGroup_ClientSiteTherapyID(
                int clientServiceGroup_ClientSiteTherapyID) {
            ClientServiceGroup_ClientSiteTherapyID = clientServiceGroup_ClientSiteTherapyID;
        }

        public String getClientName() {
            return ClientName;
        }

        public void setClientName(String clientName) {
            ClientName = clientName;
        }

        public String getTimeArrive() {
            return TimeArrive;
        }

        public void setTimeArrive(String timeArrive) {
            TimeArrive = timeArrive;
        }

        public String getTimeDepart() {
            return TimeDepart;
        }

        public void setTimeDepart(String timeDepart) {
            TimeDepart = timeDepart;
        }

        public String getSignOutTime() {
            return SignOutTime;
        }

        public void setSignOutTime(String signOutTime) {
            SignOutTime = signOutTime;
        }

        public String getSignInTime() {
            return SignInTime;
        }

        public void setSignInTime(String signInTime) {
            SignInTime = signInTime;
        }

        public String getServiceCompletedCount() {
            return ServiceCompletedCount;
        }

        public void setServiceCompletedCount(String serviceCompletedCount) {
            ServiceCompletedCount = serviceCompletedCount;
        }

        @Override
        public int compareTo(Caseload compareCaseload) {

            int busClientLogID = ((Caseload) compareCaseload).getBusClientLogID();

            return busClientLogID - this.BusClientLogID;
        }

    }

Please give me a solution.

Was it helpful?

Solution

I doubt the return statement associated with your compare function in the comparator.

You should go by this approach to get the right ordering :

   @Override
    public int compare(YourClass lhs, YourClass rhs) {

            YourClass p1 = (YourClass) lhs;
            YourClass p2 = (YourClass) rhs;

            int first = p1.ClientName; //use your getter if you want
            int second = p2.ClientName;

                if (second < first) {
                    return 1;
                }

                else if (second > first) {
                    return -1;
                }

                else {
                    return 0;
                }
        }

If you go by this approach I guess you will get the required ordering after sort.

Edit:

Now I have got the issue, you are using a reference of the original list in newBackup and its not a new list that is why this is happening, use this and you are good to go.

List<Caseload> newBackUp=new ArrayList<Caseload>(mLISTCaseloadsHeads);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top