jeudi 28 septembre 2017

Random order of specific elements in list of objects c#

In c# have a list of objects. For example a list of "cars" with attribute "color". Now I want to order randomly all cars with attribute "green" for example.

Car c01 = new Car("green","HONDA");
Car c02 = new Car("blue","BMW");
Car c03 = new Car("green","NISSAN");
Car c04 = new Car("blue","VW");
Car c05 = new Car("red","MERCEDES");
Car c06 = new Car("green","BMW");

List<CarAcceleration> Cars = new List<CarAcceleration>();
Cars.Add (c01);
Cars.Add (c02);
Cars.Add (c03);
Cars.Add (c04);
Cars.Add (c05);
Cars.Add (c06);

My idea would be to first write all green cars to a second list, reorder it and then somehow overwrite the elements in the first big list. Is there maybe a better solution? Thanks




Aucun commentaire:

Enregistrer un commentaire