| Juillet 2009 | ||||||||||
| L | M | M | J | V | S | D | ||||
| 1 | 2 | 3 | 4 | 5 | ||||||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 | ||||
| 13 | 14 | 15 | 16 | 17 | 18 | 19 | ||||
| 20 | 21 | 22 | 23 | 24 | 25 | 26 | ||||
| 27 | 28 | 29 | 30 | 31 | ||||||
|
||||||||||
|
using System;
using System.Collections.Generic;
using System.Text;
namespace TestICompare
{
public class ContactCollection :
System.Collections.Generic.List<Contact>
{
}
}
|
|
using System;
using System.Collections.Generic;
using System.Text;
namespace TestICompare
{
public class Contact :
IComparable<Contact>
{
public string Name;
public string FirstName;
public Contact(string Name, string FirstName)
{
this.Name = Name;
this.FirstName = FirstName;
}
// Implementation de IComparable
#region IComparable<Contact>
Membres
int IComparable<Contact>.CompareTo(Contact other)
{
// ordre croissant (du plus petit au plus grand) -
Aplhabètique
return this.Name.CompareTo(other.Name);
//// ordre decroissant
// return
other.Name.CompareTo(this.Name);
}
#endregion
}
}
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestICompare
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ContactCollection oContacts;
private void btnAdd_Click(object sender, EventArgs e)
{
oContacts = new ContactCollection();
oContacts.Add(new Contact("zorro", "don die"));
oContacts.Add(new Contact("Alba", "leon"));
oContacts.Add(new Contact("Durand", "pierre"));
foreach (Contact
oContact in oContacts)
{
listBox1.Items.Add(oContact.Name);
}
}
private void btnSort_Click(object sender, EventArgs e)
{
oContacts.Sort();
foreach (Contact
oContact in oContacts)
{
listBox2.Items.Add(oContact.Name);
}
}
}
}
|
|
using System;
using System.Collections.Generic;
using System.Text;
namespace TestICompare
{
public class Contact : IComparable
{
public string Name;
public string FirstName;
public Contact(string Name, string FirstName)
{
this.Name = Name;
this.FirstName = FirstName;
}
// Implementation de IComparable
#region IComparable
Membres
public
int CompareTo(object obj)
{
int nResult=0;
if (obj.GetType() != typeof(Contact))
{ }
else
{
// renvoie 1 si this.nom
commence par ex par z et nom comparer par a
// -1 inverse
nResult =this.Name.CompareTo(((Contact)obj).Name);
}
return nResult;
}
#endregion
}
}
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestICompare
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Collections.ArrayList oArray;
private void btnAdd_Click(object sender, EventArgs e)
{
oArray = new System.Collections.ArrayList();
oArray.Add(new Contact("zorro", "don die"));
oArray.Add(new Contact("Alba", "leon"));
oArray.Add(new Contact("Durand", "pierre"));
foreach (Contact
oContact in oArray)
{
listBox1.Items.Add(oContact.Name);
}
}
private void btnSort_Click(object sender, EventArgs e)
{
oArray.Sort();
foreach (Contact
oContact in oArray)
{
listBox2.Items.Add(oContact.Name);
}
}
}
}
|
|
using System;
using System.Collections.Generic;
using System.Text;
namespace TestICompare
{
public class ContactCollection :
System.Collections.Generic.List<Contact>
{
}
}
|
|
using System;
using System.Collections.Generic;
using System.Text;
namespace TestICompare
{
public class Contact : IComparable<Contact>
{
///<summary>
///
///</summary>
public class FirstNameComparer : IComparer<Contact>
{
#region IComparer<Contact> Membres
int IComparer<Contact>.Compare(Contact x, Contact y)
{
// ordre croissant -
alphabetique
return
y.FirstName.CompareTo(x.FirstName);
// ordre
decroissant
// return
x.FirstName.CompareTo(y.FirstName);
}
#endregion
}
public string Name;
public string FirstName;
public Contact(string Name, string FirstName)
{
this.Name = Name;
this.FirstName = FirstName;
}
}
}
|
|
oContacts.Sort(new Contact.FirstNameComparer());
foreach (Contact
oContact in oContacts)
{
listBox2.Items.Add(oContact.Name + " " + oContact.FirstName);
}
|
|
// sort(IndexDepart,NbElements,IComaprer<I> comparer)
oContacts.Sort(1, 2, new Contact.FirstNameComparer());
|