使用Comparator对值进行排序会更改该对象的所有值

时间:2013-10-08 06:43:19

标签: java android comparable sortedset

我在Android应用程序中工作,我想用List ObjectsObject Property进行排序。我已经成功对它进行了排序,但是当我对它进行排序时,所有带有该对象的List都将值更改为与排序值

相同

请查看ma代码:

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);

List newBackUp也将值更改为与排序列表

相同

Caseload类:

 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;
        }

    }

请给我一个解决方案。

1 个答案:

答案 0 :(得分:2)

我怀疑比较器中与比较功能相关的 return 语句。

你应该采用这种方法来获得正确的排序:

   @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;
                }
        }

如果你采用这种方法,我猜你会在排序后得到所需的订单。

编辑:

现在我遇到了问题,您使用newBackup中原始列表的引用而不是新list这就是为什么会发生这种情况,请使用此选项很高兴。

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