tags:

views:

98

answers:

3

I have class like

public class ProgressBars
{
    public ProgressBars()
    { }
    private Int32 _ID;
    private string _Name;
    public virtual Int32 ID {get { return _ID; } set { _ID = value; } }
    public virtual string Name { get { return _Name; } set { _Name = value; }}
}

here is List collection

List<ProgressBars> progress;
progress.Sort //I need to get sort here by Name

how can I sort this collection by Name?

Thanks

+2  A: 

Asked many times here:

ps: Muhammad Akhtar, MCTS - Microsoft Certified Technology Specialist
Really? Are you kidding us? MCTS doesn't know what is lambdas and how to search through MSDN?

/me shrugs

zerkms
I am not using linq instead of type collection.
Muhammad Akhtar
omg. then use .Sort() method, which obligates you to implement comparer or comparison: http://msdn.microsoft.com/en-us/library/234b841s.aspx, http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx
zerkms
and look at the links at my answer again with more attention. your comments to @Thomas's answer shows that you don't understand how LINQ and lambda works - so you following answer and msdn have to read about them.
zerkms
+6  A: 

Provided that you can use LINQ

progress = progress.OrderBy(b => b.Name).ToList();
Thomas Wanner
I am not using linq instead of type collection. I am not getting progress.orderby. This is not helpful.
Muhammad Akhtar
The question is not whether you are using LINQ but whether you can use LINQ, i.e. whether you're running on .NET 3.5. If this is the case, you might just be missing the namespace System.Linq ;)
Thomas Wanner
whats the b here?
Muhammad Akhtar
b here is an argument passed to lambda-function
zerkms
ps: just try this code before any further discussions
zerkms
Thanks alot.....
Muhammad Akhtar
+1  A: 

Implement Icomparable interface and ur done

public class ProgressBars : IComparable

{

    public ProgressBars()
    { }
    private Int32 _ID;
    private string _Name;
    public virtual Int32 ID { get { return _ID; } set { _ID = value; } }
    public virtual string Name { get { return _Name; } set { _Name = value; } }

    public int CompareTo(ProgressBars obj)
    {
        return _Name.CompareTo(obj.Name);
    }        
} 
isthatacode
yep, this is also 1 approach, but that's old and now Linq provide very cool feature and we don't need to this. I am upvoting to this 1, becoz you are right this is one way.
Muhammad Akhtar