我有一个问题是按 java 中带有比较器的两个字段进行排序
I have a problem which is sorting by two fields with comparator in java
所以我尝试按州和城市对这个 arrayList 进行排序(如果有重复的州,则比较城市)。但我无法让它工作,因为它只对州或城市进行排序。任何帮助将不胜感激。
class StateCityComparator implements Comparator<Customer>{
/**
* Comapre method that compares Customers by state and city
* @param first, second which are the numbers of the customer which needs to be compared
*/
public int compare(Customer first, Customer second){
// Getting the state
String fir = first.getState();
String sec = second.getState();
int state = fir.compareTo(sec);
int city = first.getCity().compareTo(second.getCity());
// Comparing the state
if (state < 0)
return -1;
if ( state == 0 ){ /* The problem is here */
return city;
}
return state;
}// End of compareStateCity
} // end StateCityComparator
这是输出的一部分:
Sorted by State and City:
3 Mapleview Drive Huntsville AL 358030000
2421 West Industrial Way Berkeley CA 947100000
2421 West Industrial Way Berkeley CA 947100000
4223 Halster Way Berkeley CA 947101234
4223 Halster Way Berkeley CA 947104321
4 Rocky Way Colorado Springs CO 809410000
4 Rocky Way Colorado Springs CO 809410000
5665 MassPike Circle Sandy Hook CT 064820000
45A Sturgeon Dr., Bldg. 5 Ft. Pierce FL 349510000
45A Sturgeon Dr., Bldg. 5 Ft. Pierce FL 349510000
6665 Peachtree Lane Atlanta GA 303280000
1 Washington Complex Boston MA 021010000
45521 Pilgrim Circle Nantucket MA 025540000
我认为这就是您想要的(保持代码的顶部相同):
class StateCityComparator implements Comparator<Customer> {
/**
* Compare method that compares Customers by state and city
* @param first, second which are the numbers of the customer which needs to be compared
*/
@Override
public int compare(Customer first, Customer second){
// Getting the state
String fir = first.getState();
String sec = second.getState();
int state = fir.compareTo(sec);
int city = first.getCity().compareTo(second.getCity());
// Compare the state, and the city if the states are equal
return (state == 0)? city : state;
}// End of compareStateCity
} //
或者更简洁地说,如果没有必要,请避免比较城市:
class StateCityComparator implements Comparator<Customer> {
/**
* Compare method that compares Customers by state and city
* @param first, second which are the numbers of the customer which needs to be compared
*/
@Override
public int compare(Customer first, Customer second){
int state = first.getState().compareTo(second.getState());
return (state != 0)? state : first.getCity().compareTo(second.getCity());
}// End of compareStateCity
} //
您可以很容易地为数据对象编写一个 Comparator
,使用 Comparator.comparing(...)
and Comparator.thenComparing(...)
就像这样:
public static final Comparator<Customer> STATE_CITY_COMPARATOR =
Comparator.comparing(Customer::getState).thenComparing(Customer::getCity);
所以我尝试按州和城市对这个 arrayList 进行排序(如果有重复的州,则比较城市)。但我无法让它工作,因为它只对州或城市进行排序。任何帮助将不胜感激。
class StateCityComparator implements Comparator<Customer>{
/**
* Comapre method that compares Customers by state and city
* @param first, second which are the numbers of the customer which needs to be compared
*/
public int compare(Customer first, Customer second){
// Getting the state
String fir = first.getState();
String sec = second.getState();
int state = fir.compareTo(sec);
int city = first.getCity().compareTo(second.getCity());
// Comparing the state
if (state < 0)
return -1;
if ( state == 0 ){ /* The problem is here */
return city;
}
return state;
}// End of compareStateCity
} // end StateCityComparator
这是输出的一部分:
Sorted by State and City:
3 Mapleview Drive Huntsville AL 358030000
2421 West Industrial Way Berkeley CA 947100000
2421 West Industrial Way Berkeley CA 947100000
4223 Halster Way Berkeley CA 947101234
4223 Halster Way Berkeley CA 947104321
4 Rocky Way Colorado Springs CO 809410000
4 Rocky Way Colorado Springs CO 809410000
5665 MassPike Circle Sandy Hook CT 064820000
45A Sturgeon Dr., Bldg. 5 Ft. Pierce FL 349510000
45A Sturgeon Dr., Bldg. 5 Ft. Pierce FL 349510000
6665 Peachtree Lane Atlanta GA 303280000
1 Washington Complex Boston MA 021010000
45521 Pilgrim Circle Nantucket MA 025540000
我认为这就是您想要的(保持代码的顶部相同):
class StateCityComparator implements Comparator<Customer> {
/**
* Compare method that compares Customers by state and city
* @param first, second which are the numbers of the customer which needs to be compared
*/
@Override
public int compare(Customer first, Customer second){
// Getting the state
String fir = first.getState();
String sec = second.getState();
int state = fir.compareTo(sec);
int city = first.getCity().compareTo(second.getCity());
// Compare the state, and the city if the states are equal
return (state == 0)? city : state;
}// End of compareStateCity
} //
或者更简洁地说,如果没有必要,请避免比较城市:
class StateCityComparator implements Comparator<Customer> {
/**
* Compare method that compares Customers by state and city
* @param first, second which are the numbers of the customer which needs to be compared
*/
@Override
public int compare(Customer first, Customer second){
int state = first.getState().compareTo(second.getState());
return (state != 0)? state : first.getCity().compareTo(second.getCity());
}// End of compareStateCity
} //
您可以很容易地为数据对象编写一个 Comparator
,使用 Comparator.comparing(...)
and Comparator.thenComparing(...)
就像这样:
public static final Comparator<Customer> STATE_CITY_COMPARATOR =
Comparator.comparing(Customer::getState).thenComparing(Customer::getCity);