المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Multiple Field Sorting C#



C# Programming
07-18-2009, 09:11 AM
Hi All,

I want to sort collections..


// Class

public class Item
{

public String Name{get;set;}
public int Cost{get;set;}

public Item(string name,int cost)
{
Name=name;
Cost=cost;
}
}

// Collection of Data

List items=new List();
items.Add(New Item("House",100));
items.Add(New Item("Car",80));
items.Add(New Item("Dog",10));
items.Add(New Item("Car",50));
items.Add(New Item("Cycle",25));
items.Add(New Item("Car",120));

Note:
Provide code to sort a collection of Item. The code should sort the Item collection only one time, following this rule: The Items should be ordered by their names (following the standard C# API for string ordering), but if two Items have the same name, then those Items should be ordered by their cost, with the lower cost item to appear before the higher cost item.

I appreciate somebody provide solution for the same.

Thanks & Regards
Rao