[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mono-list
Subject:    Re: [Mono-list] arraylist.sort unstable ?
From:       David Schmitt <david () dasz ! at>
Date:       2014-05-14 5:16:29
Message-ID: 5372FC2D.9010705 () dasz ! at
[Download RAW message or body]

On 2014-05-13 22:31, andreas graeper wrote:
> i am learning collections / generics ..
> and found an example defining a class Cmp that implements IComparer using
> CaseInsensitiveComparer
>
> ArrayList l = new ArrayList();
> l.Add("a");
> l.Add("A");
>
> l.Sort(new Cmp()); lprint(l);
> l.Sort(new CaseInsensitiveComparer()); lprint(l);
>
> each time i sort with either of that comparers the order of "a","A" is
> changed,
> though the Compare("a","A") returns 0.
>
> is this an error ?
> using the same comparer class twice should return the same order ?!

No. This is very much dependent on the used Sorting Algorithm. An 
algorithm that does not change the relative order of input items that 
compare equally (but are not identical) is called "stable". Sort() is 
obviously not stable.

See https://en.wikipedia.org/wiki/Sorting_algorithm#Stability


Regards, David


_______________________________________________
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic