2017-02-17 10 views
2

我有一个MasterPayee对象,基于收款人类别代码按字母顺序排序,现在我需要获得其他服务类别代码最后的排序名单使用Java比较器,但具有固定值的对象排序应该在排序列表中的最后一个值

列表排序后应用

Financial and Insurance services 

Government Sectors 

Other Services 

Telecommunications and Utilities 

Transportation Services 

必需名单如下

Financial and Insurance services 
Government Sectors 
Telecommunications and Utilities 
Transportation Services 
Other Services 

需要acheive其他服务在列表的最后比较使用对列表进行排序

Collections.sort(masterPayees, getCategoryNameComparatorByMasterPayee()); 

private Comparator<MasterPayee> getCategoryNameComparatorByMasterPayee() { 
    Comparator<MasterPayee> categoryNameComparatorByMasterPayee = new Comparator<MasterPayee>() { 
     public int compare(MasterPayee o1, MasterPayee o2) { 
      return (((MasterPayee) o1).getPayee_category().toString() 
        .compareToIgnoreCase(((MasterPayee) o2).getPayee_category().toString())); 
     } 

    }; 
    return categoryNameComparatorByMasterPayee; 
} 

其他服务应始终维持在排序列表继

+0

的可能的复制[具有部分明确的,然后另一顺序订购?](http://stackoverflow.com/questions/42125743/ordering -with-partial-explicit-and-then-another-order) – MikaelF

回答

0

您可以使用guava'a如果您知道所有可能排序的值,请进行排序。

要建立比较可以speccify你这样的价值观:

Ordering<String> ordering1 = Ordering.explicit("Financial and Insurance services","Government Sectors","Telecommunications and Utilities","Transportation Services","Other Services"); 

您也可以提供列出与你的价值观作为参数传递给Ordering.explicit()

+0

你应该为这个答案添加一些细节。 –

+1

你说得对。完成! – MicD

3

试试这个:

Comparator<MasterPayee> categoryNameComparatorByMasterPayee = new Comparator<MasterPayee>(){ 
    public int compare(MasterPayee o1, MasterPayee o2) { 
     if (((MasterPayee) o1).getPayee_category().toString().equalsIgnoreCase("Other Services") && ((MasterPayee) o1).getPayee_category().toString().equalsIgnoreCase(((MasterPayee) o2).getPayee_category().toString())) { 
      return 0; 
     } 
     else if (((MasterPayee) o1).getPayee_category().toString().equalsIgnoreCase("Other Services")) { 
      return 1; 
     } 
     else if (((MasterPayee) o2).getPayee_category().toString().equalsIgnoreCase("Other Services")) { 
      return -1; 
     } 
     else return (((MasterPayee) o1).getPayee_category().toString().compareToIgnoreCase(((MasterPayee) o2).getPayee_category().toString())); 
    } 
}; 

它把一个对象与“其他服务”始终是“大”,从而使其出现在年底。

+0

它正在按预期方式工作谢谢 – Kiran

0

我想我们可以通过使用三元表达式来优雅地处理逻辑。

private Comparator<MasterPayee> getCategoryNameComparatorByMasterPayee() { 
    Comparator<MasterPayee> categoryNameComparatorByMasterPayee = new Comparator<MasterPayee>() { 
     public int compare(MasterPayee o1, MasterPayee o2) { 
      String s1 = ((MasterPayee) o1).getPayee_category().toString(); 
      String s2 = ((MasterPayee) o1).getPayee_category().toString(); 
      boolean b1 = s1.equalsIgnoreCase("Other Services"); 
      boolean b2 = s2.equalsIgnoreCase("Other Services"); 

      return b1 ? (b2 ? 0 : 1) : (b2 ? -1 : s1.compareToIgnoreCase(s2)); 
     } 
    }; 
    return categoryNameComparatorByMasterPayee; 
} 

这避免了代码难以阅读,因此难以维护。如果我们需要改变这里的逻辑,我们可能只需要做最小的改变。

+0

这里b1和b2将返回一个int并且它会抛出编译时错误 – Kiran

+0

@Kiran对不起,我打算使用'String#equalsIgnoreCase()'。 –

0

如果这些元素只有有限的一组,我会将它们写为enum

A name为输出文本和ordinal为排序。它更干净。

0

创建一个常量映射<收款人,整数>并在比较器中使用该值。

0

另一个建议是,如果“其他服务”总是存在,将它从列表中删除,进行排序,然后最后添加“其他服务”。这样你可以保持简单的排序逻辑并单独添加异常。

如果不是总是存在,那么你可以先查找它,然后只有当它存在时才添加。

0

如果字符串列表是固定的排序是基于业务逻辑而不是字符串值,那么我推荐使用EnumMap集合。

enum Industry{ 
    FINANCE, GOVERNMENT, UTILITIES, TRANSPORT, OTHER 
} 

public class StreamEnumMap { 
    public static void main(String... strings){ 
     Map<Industry, String> industryMap = new EnumMap<>(Industry.class); 
     industryMap.put(Industry.FINANCE, "Financial and Insurance services"); 
     industryMap.put(Industry.GOVERNMENT,"Government Sectors"); 
     industryMap.put(Industry.UTILITIES,"Telecommunications and Utilities"); 
     industryMap.put(Industry.OTHER,"Other Services"); 
     industryMap.put(Industry.TRANSPORT, "Transportation Services"); 

     industryMap.values().stream().forEach(System.out::println); 
    } 
} 

这产生在下面顺序的结果,

Financial and Insurance services 
Government Sectors 
Telecommunications and Utilities 
Transportation Services 
Other Services 
+0

想法很好,但我会从DB中检索这些值。因此我不确定未来会添加多少个 – Kiran

+0

@kiran如果可以在数据库中添加优先级列,那么您可以将这两列用于排序。我个人比较喜欢数字排序比较字符串排序。进一步使用优先级列将把排序逻辑移动到数据库。这样,如果将来你必须改变顺序,那么它会更容易 – Sekar

相关问题