0

私は何を間違っていますか:

assert  'foo'  == 'foo'  //PASS
assert  '500'  == '500'  //PASS
assert  '500'  <  '1000' //FAIL <-- Supposed to pass
assert  '500'  <= '1000' //FAIL <-- Supposed to pass
assert  '1000' >  '500'  //FAIL <-- Supposed to pass
assert  '1000' >= '500'  //FAIL <-- Supposed to pass

これは、カスタマイズ可能な「条件」オブジェクト用です。

class Condition {
    static def compareClosure = [
            '==' : { a, b -> a == b},
            '!=' : { a, b -> a != b},
            '<'  : { a, b -> a <  b},
            '<=' : { a, b -> a <= b},
            '>'  : { a, b -> a >  b},
            '>=' : { a, b -> a >= b}
    ]

    String comparator
    def value

    Condition(String comparator, String value) {
        this.value = value
        this.comparator = comparator
    }

    boolean isSatisfiedBy(def value) {
        compareClosure[comparator](value, this.value)
    }
}

そう

assert new Condition('<=', '1000').isSatisfiedBy('500') //FAIL

値を数値型に変換せずにこれを行う方法はありますか?

4

1 に答える 1

0

あなたの質問に対する短い答えはノーです。

ただし、これは並べ替えのためだと感じています。もしそうなら。これが、この目的で使用するソート関数です。

実際の例: Groovy Web コンソール

    Closure customSort = { String a, String b ->
      def c = a.isBigDecimal() ? new BigDecimal(a) : a
      def d = b.isBigDecimal() ? new BigDecimal(b) : b


      if (c.class == d.class) {
        return c <=> d
      } else if (c instanceof BigDecimal) {
        return -1
      } else {
        return 1
      }

    }

['foo','500','1000', '999', 'abc'].sort(customSort)
于 2013-03-21T22:36:32.910 に答える