简体   繁体   English

用于对DropDownlist进行排序的java字符串数字比较器

[英]java String Numeric Comparator to Sort DropDownlist

String Numeric Comparator to Sort DropDownlist: 用于对DropDownlist进行排序的String Numeric Comparator

List have Ex: 1 per 1 months , 1 per 2 months ...... 2 per 12 months 列表有Ex: 1 per 1 months1 per 2 months 1 per 1 months 1 per 2 months ...... 2 per 12 months

I tried StringComparator , String Numeric Comparator , NumericComparator , Split Comparator but didn't worked,Any Suggestions 我尝试过StringComparatorString Numeric ComparatorNumericComparatorSplit Comparator但没有工作,Any Suggestions

Your custom implementation of Comparator will help you: 您的Comparator自定义实现将帮助您:

Comparator<String> comp = new Comparator<String>() {
  public int compare(String o1, String o2) {
    // TODO
  }
};

You have to write your own comparator. 你必须编写自己的比较器。 First you have to calculate x / y where the text is x per y months . 首先,您必须计算x / y,其中文本是每y个月的x Then you only have to compare x1/y1 with x2/y2. 那么你只需要将x1 / y1与x2 / y2进行比较。

Comparator<String> comp = new Comparator<String>() {
    public int compare(String o1, String o2) {
        Double d1 = calcValue(o1);
        Double d2 = calcValue(o2);
        return d1.compareTo(d2);
    }

    private Double calcValue(String s) {
         Double x, y;
         // extract x and y from String s
         ...
         return x / y;
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM