
Originariamente Scritto da
Leizar
Codice:
public class Riga : IComparable<Riga>
{
public string Testo { get; set; }
public string Roba1 { get; set; }
public string Roba2 { get; set; }
public Riga(string testo, string r1, string r2)
{
Testo = testo;
Roba1 = r1;
Roba2 = r2;
}
public int CompareTo(Riga other)
{
// return -1 if this instance precedes obj in the sort order.
// return 0 if this instance occurs in the same position in the sort order as obj.
// return 1 if this instance follows obj in the sort order.
if (this.Roba1.Equals(other.Roba1) && this.Roba2.Equals(other.Roba2)) return 0;
// metti qui le altre regole
// oppure:
Random random = new Random();
return random.Next(-1,1);
}
}
private void button1_Click(object sender, EventArgs e)
{
List<Riga> list = new List<Riga>();
list.Add(new Riga("asdfasfasfasdfkljadfkljasflkjasdfk322111111111lj", "CAZZO", "ROSSO"));
list.Add(new Riga("afrewrwe343sdfkymyaculo5ljadfkljasflkjasd3fklj", "FACCIA", "VERDE"));
list.Add(new Riga("asdfasfasfasdf342343242345kljadfkljasflkjas68", "CULO", "ROSSO"));
list.Sort();
}
Poi se Roba1 e Roba2 non sono stringhe arbitrarie ma possono assumere solo certi valori, potresti semplificarti un po' la vita con gli enum ad esempio.
CompareTo confermo. In alternativa puoi usare OrderBy sugli IEnumerable passando come argomento una funzione che definisce una chiave di priorità secondo l'ordinamento che vuoi tu, che è meno anni ottanta.
Esempio:
ALLABRACE Priorità 0
CIVAS Priorità 1
CESARINO Priorità 2
FULVIO Priorità 3
A OrderBy passerai una lambda del tipo
Codice:
x =>
{
if (x == "ALLABRACE")
return 0;
else if (x == "CIVAS")
return 1;
else if (x == "CESARINO")
return 2;
else if (x == "FULVIO")
return 3;
else
throw new System.ArgumentException("8====D~~~~");
}
Prova sto codice, se non ho capito un cazzo di quello che vuoi fare scusa ma è tardi e ho lavorato una giornata:
Codice:
namespace OrderTest
{
class Item
{
public string Code { get; set; }
public string PriorityFirst { get; private set; }
public string PrioritySecond { get; private set; }
private static Dictionary<string,int> PriorityTable =
new Tuple<string, int>[]
{
new Tuple<string, int>("CACCA",1),
new Tuple<string,int>("PUPU", 2),
new Tuple<string,int>("PIPI", 3),
new Tuple<string,int>("ALLABRACE", 0),
new Tuple<string,int>("CIVAS", 1),
new Tuple<string,int>("CESARINO", 2),
new Tuple<string,int>("FULVIO", 3)
}.ToDictionary(x => x.Item1, x => x.Item2);
public Item(string code, string priority1, string priority2)
{
Code = code;
PriorityFirst = priority1;
PrioritySecond = priority2;
}
public int Priority
{
get
{
return PriorityTable[PriorityFirst] * 10 + PriorityTable[PrioritySecond];
}
}
public override string ToString()
{
return Code + "," + PriorityFirst + "," + PrioritySecond;
}
}
class Program
{
static void Main(string[] args)
{
Item[] testList = new Item[]
{
new Item("aabaadadaebege", "PIPI", "FULVIO"),
new Item("adadaw", "PUPU", "CESARINO"),
new Item("aababege", "CACCA", "ALLABRACE"),
new Item("axxcefqfq", "CACCA", "FULVIO"),
};
var orderedList = testList.OrderBy(x => x.Priority);
foreach (var x in orderedList)
Console.WriteLine(x);
}
}
}