Skip to content

Arrays.sort #3919

@sri

Description

@sri

Is there a reason that JRuby's RubyArray uses it own hand-rolled sort (in Qsort.java) instead of using Java's builtin Arrays.sort?

I'm looking at these files:

The reason I'm asking: I encountered a bug in a piece of code. A class was overriding the <=> method, but due to a typo, it was doing this comparision:

def <=>(other); self.a <=> other.b; end

instead of

def <=>(other); self.a <=> other.a; end.

Obviously, that is a bug in the application. I didn't notice the bug because a == b most of the time. However, recently, there were cases where a != b and that's when the bug manifisted. And JRuby's sort got stuck in an infinite loop.

When I converted JRuby to use Arrays.sort instead of Qsort and tried it with the bad data, it threw an exception: "Comparison method violates its general contract!".

Also, there are other benefits of using Arrays.sort:
https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/TimSort.java#L29

Is there interest in converting RubyArray to use Arrays.sort?

Thanks!
-Sriram

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions