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

Muhammad Akhtar
  • 50,838
  • 36
  • 132
  • 186

3 Answers3

6

Provided that you can use LINQ

progress = progress.OrderBy(b => b.Name).ToList();
Tomas Vana
  • 16,537
  • 9
  • 51
  • 62
  • I am not using linq instead of type collection. I am not getting progress.orderby. This is not helpful. – Muhammad Akhtar Apr 22 '10 at 07:06
  • 1
    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 ;) – Tomas Vana Apr 22 '10 at 07:08
  • b here is an argument passed to lambda-function – zerkms Apr 22 '10 at 07:16
2

Related questions:

Community
  • 1
  • 1
zerkms
  • 230,357
  • 57
  • 408
  • 498
  • I am not using linq instead of type collection. – Muhammad Akhtar Apr 22 '10 at 07:05
  • 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 Apr 22 '10 at 07:15
  • 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 Apr 22 '10 at 07:18
1

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);
    }        
} 
Rasshme Chawla
  • 1,471
  • 2
  • 13
  • 18
  • 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 Apr 22 '10 at 13:03