codice:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
ArrayList lista = new ArrayList();
lista.Add("CAAP");
lista.Add("CABP");
lista.Add("CACP");
lista.Add("CADP");
lista.Add("CAAC");
lista.Add("CABC");
lista.Add("CACC");
lista.Add("CADC");
lista.Add("CAAI");
lista.Add("CABI");
lista.Add("CACI");
lista.Add("CADI");
lista.Add("CAAB");
lista.Add("CABB");
lista.Add("CACB");
lista.Add("CADB");
lista.Sort(new MyStringComparer());
foreach (string str in lista)
{
Console.WriteLine(str);
}
Console.Read();
}
}
public class MyStringComparer : IComparer
{
#region IComparer<string> Members
public int Compare(object objx, object objy)
{
string x = (objx as string).ToLower();
string y = (objy as string).ToLower();
switch (x[3])
{
case 'p':
if (y[3] == 'c')
return 1;
if (y[3] == 'b')
return 1;
if (y[3] == 'i')
return 1;
break;
case 'c':
if (y[3] == 'p')
return -1;
if (y[3] == 'b')
return 1;
if (y[3] == 'i')
return 1;
break;
case 'i':
if (y[3] == 'c')
return -1;
if (y[3] == 'b')
return 1;
if (y[3] == 'p')
return -1;
break;
case 'b':
if (y[3] == 'c')
return -1;
if (y[3] == 'p')
return -1;
if (y[3] == 'i')
return -1;
break;
default:
return 0;
}
return 0;
}
#endregion
}
}
devi passare un comparer al metodo sort dell'array list creato apposta per realizzare il tuo ordinamento. io non l'ho ottimizzato, ma si può migliorare il codice.