if (self instanceof RubyArray) {
RubyArray selfArray = (RubyArray) self;
final IRubyObject[][] valuesAndCriteria = new IRubyObject[selfArray.size()][2];
callEach(runtime, context, self, block.arity(), new BlockCallback() {
AtomicInteger i = new AtomicInteger(0);
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
IRubyObject larg = checkArgs(runtime, largs);
IRubyObject[] myVandC = valuesAndCriteria[i.getAndIncrement()];
myVandC[0] = larg;
myVandC[1] = block.yield(ctx, larg);
return runtime.getNil();
}
});
Arrays.sort(valuesAndCriteria, new Comparator<IRubyObject[]>() {
public int compare(IRubyObject[] o1, IRubyObject[] o2) {
return RubyFixnum.fix2int(invokedynamic(localContext, o1[1], OP_CMP, o2[1]));
}
});
IRubyObject dstArray[] = new IRubyObject[selfArray.size()];
for (int i = 0; i < dstArray.length; i++) {
dstArray[i] = valuesAndCriteria[i][0];
}
return runtime.newArrayNoCopy(dstArray);
} else {
final List<IRubyObject[]> valuesAndCriteria = new ArrayList<IRubyObject[]>();
callEach(runtime, context, self, block.arity(), new BlockCallback() {
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
IRubyObject larg = checkArgs(runtime, largs);
IRubyObject[] myVandC = new IRubyObject[2];
myVandC[0] = larg;
myVandC[1] = block.yield(ctx, larg);