Sort List C# in arbitrary order

Posted by Jasper on Stack Overflow See other posts from Stack Overflow or by Jasper
Published on 2011-11-23T17:13:39Z Indexed on 2011/11/23 17:50 UTC
Read the original article Hit count: 613

Filed under:
|
|
|
|

I have a C# List I.E. List<Food> x = new List<Food> () ;

This list is populated with this class

Public class Food {
           public string id { get; set; }
           public string idUser { get; set; }               
           public string idType { get; set; } 
          //idType could be Fruit , Meat , Vegetable , Candy 
           public string location { get; set; }    
}

Now i have this unsorted List<Food> list ; which has I.E. 15 elements. There are 8 Vegetable Types , 3 Fruit Types , 1 Meat Types , 1 Candy Types

I would sort this so that to have a list ordered in this way :

1° : Food.idType Fruit 
2° : Food.idType Vegetables 
3° : Food.idType Meat
4° : Food.idType Candy
5° : Food.idType Fruit
6° : Food.idType Vegetables
7° : Food.idType Fruit //Becouse there isnt more Meat so i insert the 
                       //next one which is Candy but also this type is empty 
                       //so i start from begin : Fruit
8° : Food.idType Vegetables
9° : Food.idType Vegetables // For the same reason of 7°
10 ° Food.idType Vegetables
......
....
....
15 : Food.idType Vegetables

I cant find a rule to do this. Is there a linq or List.Sort instruction which help me to order the list in this way?

Update i changed the return value of idType and now return int type instead string so 1=Vegetable , 2=Fruit , 3=Candy 4=Meat

© Stack Overflow or respective owner

Related posts about c#

Related posts about list