Issue Details (XML | Word | Printable)

Key: HARMONY-6076
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Sean Qiu
Reporter: Regis Xu
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Harmony

[eut][classlib][luni] - Arrays.sort(T[] a, Comparator<? super T> c) is not stable

Created: 22/Jan/09 07:34 AM   Updated: 11/Feb/09 05:43 AM
Return to search
Component/s: Classlib
Affects Version/s: 5.0M8
Fix Version/s: 5.0M9

Time Tracking:
Not Specified

File Attachments:
  Size
File Licensed for inclusion in ASF works HARMONY-6076.diff 2009-02-11 05:09 AM Regis Xu 2 kB

Resolution Date: 10/Feb/09 05:00 AM


 Description  « Hide
spec says "This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort."
but Harmony's implementation is not, which cause eut failure in org.eclipse.jdt.core.tests.compiler.test074 - 1.3

see the following test:

public class TestArrays {
    public static void main(String[] args) {
        Element[] array = new Element[11];
        array[0] = new Element(122);
        array[1] = new Element(146);
        array[2] = new Element(178);
        array[3] = new Element(208);
        array[4] = new Element(117);
        array[5] = new Element(146);
        array[6] = new Element(173);
        array[7] = new Element(203);
        array[8] = new Element(56);
        array[9] = new Element(82);
        array[10] = new Element(96);
        for (int i = 0; i < array.length; i++) {
            System.out.println("Element value is " + array[i].getValue()
                    + " index is " + array[i].getIndex());
        }

        Arrays.sort(array, new ElementComparator());

        for (int i = 0; i < array.length; i++) {
            System.out.println("Element value is " + array[i].getValue()
                    + " index is " + array[i].getIndex());
        }
    }

    public static class Element {
        private int value;

        private int index;

        public int getIndex() {
            return index;
        }

        private static int count = 0;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Element(int value) {
            this.value = value;
            index = count++;
        }
    }

    public static class ElementComparator implements Comparator {

        public int compare(Object arg0, Object arg1) {
            return ((Element) arg0).getValue() - ((Element) arg1).getValue();
        }

    }
}

notice array[1] and array[5] has the same value, the indexes are 1 and 5 corresponding, after a stable sort, element has index 1 should be ahead of element has index 5

output of RI:

=========before sort=============
Element value is 122 index is 0
Element value is 146 index is 1
Element value is 178 index is 2
Element value is 208 index is 3
Element value is 117 index is 4
Element value is 146 index is 5
Element value is 173 index is 6
Element value is 203 index is 7
Element value is 56 index is 8
Element value is 82 index is 9
Element value is 96 index is 10

=========after sort=============
Element value is 56 index is 8
Element value is 82 index is 9
Element value is 96 index is 10
Element value is 117 index is 4
Element value is 122 index is 0
Element value is 146 index is 1
Element value is 146 index is 5
Element value is 173 index is 6
Element value is 178 index is 2
Element value is 203 index is 7
Element value is 208 index is 3

output of Harmony:

=========before sort=============
Element value is 122 index is 0
Element value is 146 index is 1
Element value is 178 index is 2
Element value is 208 index is 3
Element value is 117 index is 4
Element value is 146 index is 5
Element value is 173 index is 6
Element value is 203 index is 7
Element value is 56 index is 8
Element value is 82 index is 9
Element value is 96 index is 10

=========after sort=============
Element value is 56 index is 8
Element value is 82 index is 9
Element value is 96 index is 10
Element value is 117 index is 4
Element value is 122 index is 0
Element value is 146 index is 5
Element value is 146 index is 1
Element value is 173 index is 6
Element value is 178 index is 2
Element value is 203 index is 7
Element value is 208 index is 3

notice that in Harmony index 1 and 5 is out of order after sort

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Sean Qiu added a comment - 10/Feb/09 05:00 AM
The incorrect patch is reverted at r742850.
Could you please verify it?

Thank you very much.

Regis Xu added a comment - 11/Feb/09 05:09 AM
add regression test

Sean Qiu added a comment - 11/Feb/09 05:32 AM
Thanks for your testcase.
It is applied at r743233.
Please verify if it works as you expected.

Regis Xu added a comment - 11/Feb/09 05:43 AM
Verified. Thanks Sean.