Java 自定义比较器,用于对州、县和邮政编码列表进行排序
我很难编写comparator来对具有以下字符串字段的自定义对象列表进行排序: 1.分支类-->可以是州、县或邮政编码,用于标识它是哪个数据字段;不能是空的 2.状态-->状态名称,不能为空 3.地理-->如果分支类.等于'county',它将保存县名称,可以为空 4.邮政编码-->邮政编码。对于州和县,可以为空 5.ParentNodeId-->应适用于此树的父级名称。例如,州没有父项(空字符串),而县的州作为parentNodeId,zipCode的县作为parentNodeId。因此,对于branch=“state”、zipCode“36003”的地理对象,parentNodeId应该是Autauga,state是“AL”。对于branch=“country”和geography=“Autauga”的地理对象,则parentNodeId为“AL” 当前对象列表的格式为:state-state-state-county-county-zip-code 而我正在寻找一份 -陈述 --郡 --邮政编码 --邮政编码 --郡 --邮政编码 -陈述 --郡 等等 我的审判还没有找到我不知道的案子。这是我的密码Java 自定义比较器,用于对州、县和邮政编码列表进行排序,java,algorithm,sorting,comparator,icomparable,Java,Algorithm,Sorting,Comparator,Icomparable,我很难编写comparator来对具有以下字符串字段的自定义对象列表进行排序: 1.分支类-->可以是州、县或邮政编码,用于标识它是哪个数据字段;不能是空的 2.状态-->状态名称,不能为空 3.地理-->如果分支类.等于'county',它将保存县名称,可以为空 4.邮政编码-->邮政编码。对于州和县,可以为空 5.ParentNodeId-->应适用于此树的父级名称。例如,州没有父项(空字符串),而县的州作为parentNodeId,zipCode的县作为parentNodeId。因此,对于
public static final Comparator<Geography> BY_STATE_COUNTY_ZIP_COMPARATOR = new Comparator<Geography>() {
public int compare(final Geography obj1, final Geography obj2) {
if (obj1.getZip().equals("89420") || obj2.getZip().equals("89420") || obj1.getGeography().equals("Mono")
|| obj2.getGeography().equals("Mono")) {
System.out.println("hdfhd");
}
if (obj1.getBranchClass().equalsIgnoreCase(obj2.getBranchClass())) {
return this.similarBranchComparison(obj1, obj2);
}
else {
// Different branches
final int x = this.differentBranchesComparison(obj1, obj2);
return x;
}
}
private int differentBranchesComparison(final Geography obj1,
final Geography obj2) {
if ((obj1.getZip().equals("89420") && obj1.getParentNodeId().equals("Mono"))
|| ((obj2.getZip().equals("89420") && obj2.getParentNodeId().equals("Mono")))) {
System.out.println("hdfhd");
}
// Same states - Obj1 is state
if (obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)
&& obj1.getState().equalsIgnoreCase(obj2.getState())) {
// obj2 should be greater
return -1;
}
// Same states - Obj2 is state
else if (obj2.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)
&& obj1.getState().equalsIgnoreCase(obj2.getState())) {
// obj1 should be greater
return 1;
}
// Different states - obj1 OR Obj2 is state
else if (((obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)) || (obj2
.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)))
&& !(obj1.getState().equalsIgnoreCase(obj2.getState()))) {
// Delegate to state comparison
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
// Same states - Same counties (County - Zip)
else if (obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)
&& ((obj1.getState().equalsIgnoreCase(obj2.getState())) && (obj1.getGeography()
.equalsIgnoreCase(obj2.getParentNodeId())))) {
// obj2 (zip) should be greater
return -1;
}
// Same states - Same counties (Zip - County)
else if (obj2.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)
&& ((obj1.getState().equalsIgnoreCase(obj2.getState())) && (obj1.getParentNodeId()
.equalsIgnoreCase(obj2.getGeography())))) {
// obj1 should be greater
return 1;
}
// Same states different counties (County - zip)
else if ((obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL))
&& (obj1.getState().equalsIgnoreCase(obj2.getState()) && !(obj1.getGeography()
.equalsIgnoreCase(obj2.getParentNodeId())))) {
return new CompareToBuilder().append(obj1.getGeography(), obj2.getParentNodeId()).toComparison();
}
// Same states different counties (Zip - County)
else if ((obj2.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL))
&& (obj1.getState().equalsIgnoreCase(obj2.getState()) && !(obj1.getParentNodeId()
.equalsIgnoreCase(obj2.getGeography())))) {
return new CompareToBuilder().append(obj1.getParentNodeId(), obj2.getGeography()).toComparison();
}
// Different States
else if (((obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)) || (obj2
.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)))
&& !(obj1.getState().equalsIgnoreCase(obj2.getState()))) {
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
return 0;
}
private int similarBranchComparison(final Geography obj1,
final Geography obj2) {
// State-State, County - County, Zip-Zip
// State-State
if (obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)) {
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
// County - County
else if (obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)) {
if (obj1.getState().equalsIgnoreCase(obj2.getState())) {
// Compare Counties within the same state
return new CompareToBuilder().append(obj1.getGeography(), obj2.getGeography()).toComparison();
}
else {
// Compare Counties within different states
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
}
else {
// Zip - Zip
if (obj1.getState().equalsIgnoreCase(obj2.getState())) {
if (obj1.getParentNodeId().equalsIgnoreCase(obj2.getParentNodeId())) {
return new CompareToBuilder().append(obj1.getZip(), obj2.getZip()).toComparison();
}
else {
return new CompareToBuilder().append(obj1.getParentNodeId(), obj2.getParentNodeId())
.toComparison();
}
}
else {
// Compare Zip codes within different states
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
}
}
};
public static final Comparator BY_STATE_COUNTY_ZIP_Comparator=new Comparator(){
公共整数比较(最终地理obj1、最终地理obj2){
如果(obj1.getZip().equals(“89420”)|| obj2.getZip().equals(“89420”)| obj1.getGeography().equals(“Mono”)
||obj2.getGeography().equals(“Mono”)){
系统输出打印项次(“hdfhd”);
}
if(obj1.getBranchClass().equalsIgnoreCase(obj2.getBranchClass())){
返回此.similarBranchComparison(obj1,obj2);
}
否则{
//不同的分支
最终int x=此差异分支比较(obj1,obj2);
返回x;
}
}
私人内部不同分支机构比较(最终地理位置),
最终地理信息(J2){
if((obj1.getZip().equals(“89420”)和&obj1.getParentNodeId().equals(“Mono”))
||((obj2.getZip().equals(“89420”)和&obj2.getParentNodeId().equals(“Mono”)){
系统输出打印项次(“hdfhd”);
}
//相同状态-Obj1为状态
if(obj1.getBranchClass().equalsIgnoreCase(ASDGegraphyBasedDatashBoardVO.STATE_级别)
&&obj1.getState().equalsIgnoreCase(obj2.getState()){
//obj2应该更大
返回-1;
}
//相同状态-Obj2为状态
else if(obj2.getBranchClass().equalsIgnoreCase(ASDGegraphyBasedDatashBoardVO.STATE_级别)
&&obj1.getState().equalsIgnoreCase(obj2.getState()){
//obj1应该更大
返回1;
}
//不同的状态-obj1或Obj2为状态
else if(((obj1.getBranchClass().equalsIgnoreCase(asdGeographyBasedAshBoardVO.STATE_LEVEL))| |(obj2
.getBranchClass()
&&!(obj1.getState().equalsIgnoreCase(obj2.getState())){
//委托到状态比较
返回新的CompareToBuilder().append(obj1.getState(),obj2.getState()).toComparison();
}
//相同州-相同县(县-邮政编码)
else if(obj1.getBranchClass().equalsIgnoreCase(asdGeographyBasedAshBoardVO.COUNTY_级别)
&&((obj1.getState().equalsIgnoreCase(obj2.getState())&(obj1.getGeography())
.equalsIgnoreCase(obj2.getParentNodeId()){
//obj2(zip)应该更大
返回-1;
}
//相同州-相同县(邮编-县)
else if(obj2.getBranchClass().equalsIgnoreCase(asdGeographyBasedAshBoardVO.COUNTY_级别)
&&((obj1.getState().equalsIgnoreCase(obj2.getState())&&(obj1.getParentNodeId())
.equalsIgnoreCase(obj2.getGeography()){
//obj1应该更大
返回1;
}
//同一州不同县(县-zip)
else if((obj1.getBranchClass().equalsIgnoreCase(asdGeographyBasedAshBoardVO.COUNTY_LEVEL))
&&(obj1.getState().equalsIgnoreCase(obj2.getState())和&!(obj1.getGeography())
.equalsIgnoreCase(obj2.getParentNodeId()){
返回新的CompareToBuilder().append(obj1.getGeography(),obj2.getParentNodeId()).toComparison();
}
//同一州不同县(Zip-县)
else if((obj2.getBranchClass().equalsIgnoreCase(asdGeographyBasedAshBoardVO.COUNTY_LEVEL))
&&(obj1.getState().equalsIgnoreCase(obj2.getState())和&!(obj1.getParentNodeId()
.equalsIgnoreCase(obj2.getGeography()){
返回新的CompareToBuilder().append(obj1.getParentNodeId(),obj2.getGeography()).toComparison();
}
//不同的国家
如果((obj1.getBranchClass().equalsIgnoreCase(asdGeographyBasedAshBoardVO.COUNTY_LEVEL))| |(obj2
.getBranchClass()
&&!(obj1.getState().equalsIgnoreCase(obj2.getState())){
返回新的CompareToBuilder().append(obj1.getState(),obj2.getState()).toComparison();
}
返回0;
}
私人类似分支机构比较(最终地理信息),
最终地理信息(J2){
//州-州,县-县,邮编
//州
if(obj1.getBranchClass().equalsIgnoreCase(ASDGegraphyBasedDatashBoardVO.STATE_级别)){
返回新的CompareToBuilder().append(obj1.getState(),obj2.getState()).toComparison();
}
//县-县
else if(obj1.getBranchClass().equalsIgnoreCase(asdGeographyBasedAshBoardVO.COUNTY_LEVEL)){
if(obj1.getState().equalsIgnoreCase(obj2.getState())){
//比较同一州内的县
返回新的CompareToBuilder().append(obj1.getGeography(),obj2.getGeography()).toComparison();
}
否则{
//比较不同州的县
R
Comparator<Geography> comparator = new Comparator<>() {
public int compare(final Geography obj1, final Geography obj2) {
state1 = getState(obj1);
state2 = getState(obj2);
int retCode = state1.compare(state2);
if (retCode != 0)
return retCode;
county1 = getCounty(obj1);
county2 = getCounty(obj2);
retCode = county1.compare(county2);
if(retCode != 0)
return retCode;
zip1 = getZip(obj1);
zip2 = getZip(obj2);
retCode = zip1.compare(zip2);
return retCode;
}
}