与比较值排序都在变化值与该对象 [英] Sorting values with Comparator changes all the values with that object

查看:118
本文介绍了与比较值排序都在变化值与该对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用程序的工作我想排序列表 对象对象属性。我已经成功地进行排序,但是当我所有的列表与该对象进行排序它改变了价值相同排序值

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

请看看马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);

列表newBackUp也改变了价值相同的排序列表

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

案件量类:

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

    }

请给我一个解决方案。

推荐答案

我怀疑在收益语句在比较的比较功能相关联。

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.

编辑:

现在我有这个问题,你在 newBackup 使用原始列表的引用,它不是一个新的列表这就是为什么发生这种情况,使用这个,你是好去。

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.

列表&LT;案件量&GT; newBackUp =新的ArrayList&LT;案件量&GT;(mLISTCaseloadsHeads);

这篇关于与比较值排序都在变化值与该对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆