[Mono-bugs] [Bug 442257] New: System.Array.Sort produces wrong results

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Thu Nov 6 07:43:12 EST 2008


https://bugzilla.novell.com/show_bug.cgi?id=442257


           Summary: System.Array.Sort produces wrong results
           Product: Mono: Class Libraries
           Version: 2.0
          Platform: x86
        OS/Version: Mac OS X 10.5
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: System
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: fredrik.kling at swissqual.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---


It seems like the Sort(Array, IComparer), in System.Array.Sort, function does
not swap when '0' is returned. However I think MS implementation does...
When comparing int's the following statement produces a wrongly sorted order,
compared to MS implementation (should be greatest first)

See further down for complete implementation, the various return statements
here are from the Compare function in the IComparer.

Ex1:
return ((ix > iy ? 1 : 0));
Output differs, MS is expected mono is NOT sorted (leafs are unswapped).

Ex2:
"return ((ix > iy ? -1 : 1));"
Returns expected order, and same result using both MS and Mono.

Ex3:
"return ((ix > iy ? 1 : -1));"
MS throws exception, Mono does not...

Unhandled Exception: System.ArgumentException: IComparer (or the IComparable
methods it relies upon) did not return zero when Ar
ray.Sort called x. CompareTo(x). x: '1'  x's type: 'Int32' The IComparer:
'SortTest.MyComp'.
   at System.Array.SorterGenericArray.QuickSort(Int32 left, Int32 right)
   at System.Array.SorterGenericArray.QuickSort(Int32 left, Int32 right)
   at System.Array.Sort(Array keys, Array items, Int32 index, Int32 length,
IComparer comparer)
   at System.Array.Sort(Array array, IComparer comparer)


Tested on: 
- Mac OsX 10.5.4, Mono 2.0.1
- Win XP, Mono 1.9.1

Referenced against MS .Net 2.0

Examples:
C:\>SortTest.exe
10
8
7
5
1

[this produces the same result on the Mac]
c:\>mono\mono.exe SortTest.exe
8
10
7
1
5
-----
Test program:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace SortTest
{
    public class MyComp : IComparer
    {
        #region IComparer Members

        public int Compare(object x, object y)
        {
            int ix, iy;
            ix = (int)x;
            iy = (int)y;
            // This should throw an exception!
            // return ((ix > iy ? 1 : -1));
            // This should provide reversed order (Greatest first)
            return ((ix > iy ? 1 : 0));
        }

        #endregion
    }
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = new int[] { 1, 5, 7, 8, 10 };
            System.Array.Sort(num,new MyComp());
            for (int i = 0; i < num.Length; i++)
            {
                System.Console.WriteLine("{0}", num[i]);

            }

        }
    }
}


-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list