Sei sulla pagina 1di 10

PCS4

Week 3:
Sorting the elements in a list
Sorting the elements of a list, like last
week we did for an array:
for ( int k=0; k< myList.Count; k++)
{
"find the smallest of the remaining elements";
"swap this smallest element with the k-th element"
}

To find the smallest of the remaining elements: keep comparing two


elements, until you found the smallest.
So, repeatingly: compare 2 elements, say e1 and e2:
there are 3 options:
• e1 should be ordered before e2,
• e1 is equal to e2,
• e1 should be ordered after e2.

2
There are several sorting algorithms:

See on internet: https://www.toptal.com/developers/sorting-algorithms

3
The method Sort() of the List-class
(copied from the Microsoft-Help)
Sort() Sorts the elements in the
entire List<T> using the default
comparer.
Sort(Comparison<T>) Sorts the elements in the
entire List<T> using the specified 
System.Comparison<T>.

Sort(IComparer<T>) Sorts the elements in the


entire List<T> using the specified
comparer.
Sort(Int32, Int32, IComparer Sorts the elements in a range of
<T>) elements in List<T> using the specified
comparer.

4
The method Sort() of the List-class
(copied from the Microsoft-Help)
Sort() Elements in the list Sorts the elements in the
implement the entire List<T> using the default
IComparable- comparer.
interface
Sort(Comparison<T>) Sorts the elements in the
entire List<T> using the specified 
A delegate
System.Comparison<T>.
Comparison
Sort(IComparer<T>) Sorts the elements in the
Something that entire List<T> using the specified
implements the comparer.
IComparer-interface
Sort(Int32, Int32, IComparer Sorts the elements in a range of
<T>) elements in List<T> using the specified
comparer.

5
The method Sort() of the List-class
1. The interface IComparable [ or: IComparable<T> ] has a method:
int CompareTo(Object other)

[ or: int CompareTo(T other) ]

2. The delegate Comparison [ or: Comparison<T> ]:


public delegate int Comparison(Object x, Object y);

[ or: public delegate int Comparison<in T>(T x, T y); ]

3. The interface IComparer [ or: IComparer<T> ] has a method:


int Compare(Object x, Object y)

[ or: int Compare(T x, T y) ]

6
The method Sort() of the List-class
1. The interface IComparable [ or: IComparable<T> ] has a method:
int CompareTo(Object other)

[ or: int CompareTo(T other) ]

2. The delegate Comparison [ or: Comparison<T> ]:


public delegate int Comparison(Object x, Object y);

[ or: public delegate int Comparison<in T>(T x, T y); ]

3. The interface IComparer [ or: IComparer<T> ] has a method:


int Compare(Object x, Object y)

[ or: int Compare(T x, T y) ] Demonstrated today

7
1. The default Sort() of the List-class
Suppose we have a list myDrumbles (which is a list of Drumble-objects).
Then:

myDrumbles.Sort();

performs the default sorting method of the list, if the class Drumble
implements one of the IComparable-interfaces
(the “normal” IComparable or, in this case, IComparable<Drumble>).

The “normal” IComparable has a method int CompareTo(Object other)


returns a negative value if this should be ordered before other,
returns 0 if this and other are equal,
returns a positive value if this should be ordered after other.

Demo: show it ! ! !

8
2. Sort-method of the List-class, using a
delegate
Again, suppose we have the list myDrumbles.

Then:

this.myDrumbles.Sort(
new Comparison<Drumble>( XXXX ) );

performs a sorting method of the list, if XXXX is method which fits to the
prototype of the Comparison<Drumble> delegate (or fits to the prototype
of the Comparison delegate).

Demo: show it ! ! !

9
3. Another Sort-method of the List-class,
using an object that implements the
IComparer-interface
Again, suppose we have the list myDrumbles.

Then:

myDrumbles.Sort( ????? );

performs a sorting method of the list, if ????? is an object (but not a


mysterious delegate-object).

This object could be of any type, but it must implement the


IComparer<Drumble> - interface (or the IComparer – interface).
So it should implement a method
int Compare(Drumble firstObject, Drumble secondObject)

Demo: show it ! ! !
10

Potrebbero piacerti anche