java - Comparator not sorting properly -



java - Comparator not sorting properly -

i trying write test set under stress behaviour of next class. comparator website, reason section of 'propertiescreatedby' not working. unusual issue long rest of fields working , can sorted properly, when trying sort column order seems 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; (int = 0; < properties.length; i++) { javabeannamedpropertycomparator comparator = new javabeannamedpropertycomparator(properties[i], shouldreverse[i]); result = comparator.compare(obj1, obj2); if(result != comparatorconstants.equal) { homecoming result; } } //actually object equals.... homecoming comparatorconstants.less_than; } public void setdescending(boolean descending) { (int = 0; < 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) { homecoming comparator.compare(obj1, obj2); } }

i spent several hours looking @ code , making experiments can't find what's wrong. more implementing test class, i'd prepare it, can't find problem. appreciate hint.

thanks!

my guess: info problem. homecoming comparatorconstants.less_than on equal values (why?) means equivalent info ordered arbitrarily.

returning less-than on equal values risks introducing infinite loops if sorting algorithm isn't tolerant of case.

are sure these fields correctly populated, , you're referring them right name?

java unit-testing sorting tdd comparator

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -