与比较排序值将所有数值与该对象(Sorting values with Comparator cha

2019-10-18 17:53发布

我在Android应用程序,我想一个排序工作ListObjectsObject Property 。 我已经成功地整理,但是当我所有的列表与该对象进行排序它的价值,以同样的变化排序值

请考虑马代号:

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也改变了价值一样排序列表

案件量类:

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

    }

请给我一个解决方案。

Answer 1:

我怀疑,在比较的比较功能相关的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);



文章来源: Sorting values with Comparator changes all the values with that object