gpt4 book ai didi

java - 自定义比较器,用于对州、县和邮政编码列表进行排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:27:39 26 4
gpt4 key购买 nike

我很难编写比较器来对具有以下字符串字段的自定义对象列表进行排序: 1. Branch Class --> 可以是state, county or zip code 用于标识是哪个数据字段;不能为空 2. State --> State Name,不能为空 3. Geography --> 如果branch class.equals'county',它会保存县名,可以为空 4. 邮政编码 --> 邮政编码。州和县可以为空 5. ParentNodeId --> 适用于该树的父节点的名称。例如,州没有父节点(空字符串),而县的父节点为州,邮政编码的父节点为县。因此,对于 branch = "state"、zipCode "36003"的地理对象,parentNodeId 应该是 Autauga,state 是 "AL"。对于 branch ="county"和 geography ="Autauga"的地理对象,则 parentNodeId 为“AL”。

当前对象列表的形式为:state - state - state - county - county - zip code - zip code

虽然我正在寻找一个列表

-状态

-- 县

-- zip

-- zip

-- 县

-- zip

-状态

-- 县

等等。

我的审判仍然遗漏了我不知道的案例。这是我的代码

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

}
}
};

最佳答案

这应该是一个简单的比较器,您首先比较州,然后是县,然后是 zip 。因此,这假设您可以编写 getState()、getCounty() 和 getZip() 方法,并且可以编写州、县和邮政编码的比较。如果您不能从所有不同的类型中获取状态,那么您就无法比较它们。

这是一些伪代码。

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

关于java - 自定义比较器,用于对州、县和邮政编码列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22724872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com