简体   繁体   English

比较器未正确排序

[英]Comparator not sorting properly

I am trying to write a test to put under stress the behaviour of the following class.我正在尝试编写一个测试来强调以下类的行为。 It is a comparator for a website, but for some reason the section of 'propertiesCreatedBy' is not working.它是网站的比较器,但由于某种原因,“propertiesCreatedBy”部分不起作用。 This is a very strange issue as long as the rest of fields are working and can be sorted properly, but when trying to sort by this column the order seems to be random:只要其余字段都在工作并且可以正确排序,这是一个非常奇怪的问题,但是当尝试按此列排序时,顺序似乎是随机的:

import java.io.Serializable;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;


public class CaseNoteDTOComparator implements Comparator, Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    protected class CaseNoteDTOComparatorInternal implements Serializable  {
        private String[] properties = null;
        boolean[] shouldReverse = null;

        public CaseNoteDTOComparatorInternal(String[] properties, boolean[] shouldReverse) {
            this.properties = properties;
            this.shouldReverse = shouldReverse;
        }

        public int compare(Object obj1, Object obj2) {
            int result = 0;
            for (int i = 0; i < properties.length; i++) {
                JavaBeanNamedPropertyComparator comparator =
                    new JavaBeanNamedPropertyComparator(properties[i], shouldReverse[i]);
                result = comparator.compare(obj1, obj2);
                if(result != ComparatorConstants.EQUAL) {
                    return result;
                }
            }
            //actually object are equals....
            return ComparatorConstants.LESS_THAN;
        }

        public void setDescending(boolean descending) {
            for (int i = 0; i < shouldReverse.length; i++) {
                shouldReverse[i] = descending;
            }
        }
    }

    private static Map comparators = new HashMap();

    //statically initialise comparators
    {
        final String[] propertiesEffectiveDate = { EFFECTIVE_DATE_NAME, CREATED_ON };
        boolean[] shouldReverseEffectiveDate= {false, false};
        comparators.put(EFFECTIVE_DATE_NAME, new CaseNoteDTOComparatorInternal(propertiesEffectiveDate, shouldReverseEffectiveDate));

        final String[] propertiesId = { ID_NAME };
        boolean[] shouldReverseId = {false};
        comparators.put(ID_NAME, new CaseNoteDTOComparatorInternal( propertiesId, shouldReverseId));

        final String[] propertiesType = { TYPE_NAME, ID_NAME };
        boolean[] shouldReverseType = {false, false};
        comparators.put(TYPE_NAME, new CaseNoteDTOComparatorInternal( propertiesType, shouldReverseType));

        final String[] propertiesTitle = { TITLE_NAME, ID_NAME };
        boolean[] shouldReverseTitle = {false, false};
        comparators.put(TITLE_NAME, new CaseNoteDTOComparatorInternal( propertiesTitle, shouldReverseTitle));

        final String[] propertiesRecordedVS = { PERSON_LASTNAME_NAME, PERSON_FIRSTNAME_NAME, ID_NAME };
        boolean[] shouldReverseRecordedVS= {false, false, false};
        comparators.put(RECORDED_VS, new CaseNoteDTOComparatorInternal( propertiesRecordedVS, shouldReverseRecordedVS));

        final String[] propertiesCreatedBy = { CREATED_BY_LASTNAME_NAME, CREATED_BY_FIRSTNAME_NAME, ID_NAME };
        boolean[] shouldReverseCreatedBy= {false, false, false};
        comparators.put(CREATED_BY_NAME, new CaseNoteDTOComparatorInternal( propertiesCreatedBy, shouldReverseCreatedBy));

        final String[] propertiesOrganisation = { ORGANISATION_NAME, ID_NAME };
        boolean[] shouldReverseOrganisation= {false, false};
        comparators.put(ORGANISATION_NAME, new CaseNoteDTOComparatorInternal( propertiesOrganisation, shouldReverseOrganisation));

        final String[] propertiesScore = { LUCENE_INDEX_SCORE};
        boolean[] shouldReverseScore = {false};
        comparators.put(LUCENE_INDEX_SCORE, new CaseNoteDTOComparatorInternal( propertiesScore, shouldReverseScore));

    }

    private CaseNoteDTOComparatorInternal comparator = null;

    public static final String ID_NAME = "id";
    public static final String EFFECTIVE_DATE_NAME = "effectiveDate";
    public static final String TYPE_NAME = "displayCaseNoteType";
    public static final String TITLE_NAME = "title";
    public static final String PERSON_LASTNAME_NAME = "personLastName";
    public static final String PERSON_FIRSTNAME_NAME = "personFirstName";
    public static final String RECORDED_VS = "recordedVS";
    public static final String CREATED_BY_NAME = "createdBy";
    public static final String CREATED_BY_FIRSTNAME_NAME = "createdByFirstName";
    public static final String CREATED_BY_LASTNAME_NAME =  "createdByLastName";
    public static final String CREATED_ON = "createdOn";
    public static final String ORGANISATION_NAME = "organisationName";
    public static final String DEFAULT_FIELD_NAME = EFFECTIVE_DATE_NAME;
    public static final String LUCENE_INDEX_SCORE = "score";

    public CaseNoteDTOComparator(String fieldKeyName, boolean descending) {
        this.comparator = (CaseNoteDTOComparatorInternal)comparators.get(fieldKeyName);
        if(comparator==null) {
            this.comparator = (CaseNoteDTOComparatorInternal)comparators.get(DEFAULT_FIELD_NAME);
        }
        comparator.setDescending(descending);
    }

    public int compare(Object obj1, Object obj2) {

        return comparator.compare(obj1, obj2);
    }
}

I spent several hours looking at the code and making experiments but can't find what's wrong.我花了几个小时查看代码并进行实验,但找不到问题所在。 More than implementing the test for the class, I'd like to fix it, but can't find the problem.除了为班级实施测试之外,我还想修复它,但找不到问题。 I will really appreciate any hint.我会很感激任何提示。

Thanks!谢谢!

My guess: this is a data problem.我的猜测:这是一个数据问题。 You return ComparatorConstants.LESS_THAN on equal values (why?) which means that equivalent data will be ordered arbitrarily.您以相等的值返回ComparatorConstants.LESS_THAN (为什么?),这意味着等效数据将被任意排序。

Returning less-than on equal values also risks introducing infinite loops if the sorting algorithm isn't tolerant of that case.如果排序算法不能容忍这种情况,则在相等的值上返回小于也有引入无限循环的风险。

Are you sure these fields are correctly populated, and you're referring to them by the correct name?您确定这些字段已正确填充,并且您使用正确的名称来引用它们吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM