Multiple Field Sorting C#
-
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
-
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
-
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
Use this
public class Item : IComparable
{public String Name{get;set;}
public int Cost{get;set;}
public Item(string name,int cost)
{
Name=name;
Cost=cost;
}int Icomparable.CompareTo(object obj)
{
Item other = (Item) obj;
If this.Name < other.Name return -1;
If this.Name > other.Name return 1;
If this.Cost > other.Cost return 1;
If this.Cost < other.Cost return -1;
return 0;
}
}Then you can just do items.Sort() Probably worth having a look at IComparable and IComparer interfaces. You could alsi look at using List in generics namespace.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
-
Use this
public class Item : IComparable
{public String Name{get;set;}
public int Cost{get;set;}
public Item(string name,int cost)
{
Name=name;
Cost=cost;
}int Icomparable.CompareTo(object obj)
{
Item other = (Item) obj;
If this.Name < other.Name return -1;
If this.Name > other.Name return 1;
If this.Cost > other.Cost return 1;
If this.Cost < other.Cost return -1;
return 0;
}
}Then you can just do items.Sort() Probably worth having a look at IComparable and IComparer interfaces. You could alsi look at using List in generics namespace.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
riced wrote:
Item other = (Item) obj; If this.Name < other.Name return -1; If this.Name > other.Name return 1; If this.Cost > other.Cost return 1; If this.Cost < other.Cost return -1; return 0;
Item other = (Item) obj;
int diff=string.Compare(this.Name, other.Name);
if (diff==0) diff=this.Cost-other.Cost;
return diff;And using a generic IComparable, you wouldn't need the cast. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
riced wrote:
Item other = (Item) obj; If this.Name < other.Name return -1; If this.Name > other.Name return 1; If this.Cost > other.Cost return 1; If this.Cost < other.Cost return -1; return 0;
Item other = (Item) obj;
int diff=string.Compare(this.Name, other.Name);
if (diff==0) diff=this.Cost-other.Cost;
return diff;And using a generic IComparable, you wouldn't need the cast. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
That's much neater. :) Looking at the OPs code I think they are a beginner, that's why I did it like I did and suggested looking at the interfaces.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
-
-
riced wrote:
Item other = (Item) obj; If this.Name < other.Name return -1; If this.Name > other.Name return 1; If this.Cost > other.Cost return 1; If this.Cost < other.Cost return -1; return 0;
Item other = (Item) obj;
int diff=string.Compare(this.Name, other.Name);
if (diff==0) diff=this.Cost-other.Cost;
return diff;And using a generic IComparable, you wouldn't need the cast. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.