icomparable interface

iComparable interface is used for sorting arrays of your objects. If you implement the icomparable interface you need to add the CompareTo method to your class. In this method you can compare the properties of your object. When an array of these objects is created and the sort method is executed the array of method is sorted according the compated properties wich you defined in the Compare to method.

Example: 

    public class PersonenTest
    {
        public ArrayList personen = new ArrayList ();
        public PersonenTest()
        {
                int x = 0;
                Random random = new Random(1);
                while (x++ < 100)
	            {
                    int nummer = random.Next(100);
			        personen.Add( new Personen(){ Naam = nummer.ToString(), Leeftijd = nummer}); 
			    }
        }

        public void SortPersonen()
        {
            personen.Sort();
        }
    }

    public class Personen:IComparable
    {

        public string Naam { get; set; }
        public int Leeftijd { get; set; }

        public int CompareTo(object obj)
        {
            if (obj == null) throw new ArgumentNullException("Object is leeg");

            Personen personen = (Personen)obj;
            return this.Leeftijd.CompareTo(personen.Leeftijd);
        }
    }

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!