Exemple #1
0
  @org.python.Method(
      __doc__ = "L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*",
      args = {},
      default_args = {"key", "reverse"})
  public org.python.Object sort(final org.python.Object key, org.python.Object reverse) {
    if (key == null && reverse == null) {
      Collections.sort(this.value);
    } else {
      // needs to be final in order to use inside the comparator
      final boolean shouldReverse =
          reverse == null ? false : ((org.python.types.Bool) reverse.__bool__()).value;

      Collections.sort(
          this.value,
          new Comparator<org.python.Object>() {
            @Override
            public int compare(org.python.Object o1, org.python.Object o2) {
              org.python.Object val1 = o1;
              org.python.Object val2 = o2;
              if (key != null) {
                val1 = ((org.python.types.Function) key).invoke(o1, null, null);
                val2 = ((org.python.types.Function) key).invoke(o2, null, null);
              }
              return shouldReverse ? val2.compareTo(val1) : val1.compareTo(val2);
            }
          });
    }
    return org.python.types.NoneType.NONE;
  }