我有一个问题,即在java中使用比较器按两个字段排序



所以我正在尝试按州和城市对这个数组列表进行排序(如果有重复的州,则比较城市(。但我无法让它工作,因为它只对州或城市进行排序。任何帮助将不胜感激。

 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

您可以非常轻松地为数据对象编写Comparator,使用如下所示的Comparator.comparing(...)Comparator.thenComparing(...)

public static final Comparator<Customer> STATE_CITY_COMPARATOR =
    Comparator.comparing(Customer::getState).thenComparing(Customer::getCity);

我认为这就是你想要的(保持代码的顶部相同(:

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

最新更新