1

I need to make ascending sort of generic List. Here is my generic type:

public class ContainerData { private Point pnt; private Size size; private Double Area; private Contour<Point> contour; public ContainerData() { } public ContainerData(ContainerData containerData) { this.pnt=containerData.pnt; this.size=containerData.size; this.Area=containerData.Area; this.contour = containerData.contour; } public Point pointProperty { get {return pnt;} set {pnt = value;} } public Size sizeProperty { get {return size;} set {size = value;} } public Double AreaProperty { get {return Area;} set {Area = value;} } public Contour<Point> ContourProperty { get {return contour;} set { contour = value; } } } 

The ascending sorting have to be made under the value of X coordinate of th Point type.

Here is the class member that sholud be sorted:

 private List<ContainerData> dataOfPreviusImage; 

Any idea how can I implement it?

Thank you in advance.

3
  • 2
    What have you tried? Is List.Sort that difficult to find? Commented Oct 11, 2012 at 8:12
  • 1
    If you dont want to use LINQ I suggest you to read this msdn page: msdn.microsoft.com/en-us/library/w56d4y5z.aspx Commented Oct 11, 2012 at 8:13
  • You can also look into this link also Commented Oct 11, 2012 at 8:24

3 Answers 3

9

Use Enumerable.OrderBy :

List<ContainerData> dataOfPreviusImage = GetSomeData(); var sorted = dataOfPreviusImage.OrderBy(cd => cd.pointProperty.X); 
Sign up to request clarification or add additional context in comments.

Comments

2

Make a Compare Function :

private int SortFunction(ContainerData obj1, ContainerData obj2) { return obj1.pointProperty.X.CompareTo(obj2.pointProperty.X); } 

And call it like this:

dataOfPreviusImage.Sort(new Comparison<ContainerData>(SortFunction)); 

1 Comment

inline version: dataOfPreviusImage.Sort((obj1, obj2) => obj1.pointProperty.X.CompareTo(obj2.pointProperty.X));
1

To help you along your way, your ContainerData class should implement IComparable and ideally IEquateable as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.