private synchronized void sort(PyObject cmp, PyObject key, boolean reverse) {
    gListAllocatedStatus = -1;

    int size = list.size();
    final ArrayList<KV> decorated = new ArrayList<KV>(size);
    for (PyObject value : list) {
      decorated.add(new KV(key.__call__(value), value));
    }
    list.clear();
    KVComparator c = new KVComparator(this, cmp);
    if (reverse) {
      Collections.reverse(decorated); // maintain stability of sort by reversing first
    }
    Collections.sort(decorated, c);
    if (reverse) {
      Collections.reverse(decorated);
    }
    if (list instanceof ArrayList) {
      ((ArrayList) list).ensureCapacity(size);
    }
    for (KV kv : decorated) {
      list.add(kv.value);
    }
    gListAllocatedStatus = __len__();
  }
 public int compare(PyObject o1, PyObject o2) {
   int result = cmp.__call__(o1, o2).asInt();
   if (this.list.gListAllocatedStatus >= 0) {
     throw Py.ValueError("list modified during sort");
   }
   return result;
 }
 public int compare(KV o1, KV o2) {
   int result;
   if (cmp != null && cmp != Py.None) {
     result = cmp.__call__(o1.key, o2.key).asInt();
   } else {
     result = o1.key._cmp(o2.key);
   }
   if (this.list.gListAllocatedStatus >= 0) {
     throw Py.ValueError("list modified during sort");
   }
   return result;
 }
 public PyObject __call__(PyObject[] args, String[] keywords) {
   if (im_self != null)
     // bound method
     return im_func.__call__(im_self, args, keywords);
   // unbound method.
   boolean badcall = false;
   if (im_class == null)
     // TBD: An example of this is running any function defined in
     // the os module.  If you "import os", you'll find it's a
     // jclass object instead of a module object.  Still unclear
     // whether that's wrong, but it's definitely not easily fixed
     // right now.  Running, e.g. os.getcwd() creates an unbound
     // method with im_class == null.  For backwards compatibility,
     // let this pass the call test
     ;
   else if (args.length < 1) badcall = true;
   else
     // xxx can be faster?
     // first argument must be an instance who's class is im_class
     // or a subclass of im_class
     badcall = !__builtin__.issubclass(args[0].fastGetClass(), im_class);
   if (badcall) {
     String got = "nothing";
     if (args.length >= 1) got = class_name(args[0].fastGetClass()) + " instance";
     throw Py.TypeError(
         "unbound method "
             + __name__
             + "() must be "
             + "called with "
             + class_name(im_class)
             + " instance as first argument"
             + " (got "
             + got
             + " instead)");
   } else return im_func.__call__(args, keywords);
 }