The best approach is to pass the criteria by which you can order. You can use the following code as a motivation:
public void DoSomething<T>( .... , Func<Point, T> orderbySelector) { foreach( ... OrderBy(p => orderbySelector(p.Location))) { ... } }
Now you can:
DoSomething(mySequence, point => point.X)
or
DoSomething(mySequence, point => point.Y)
Note. You can generalize the selector as much as you want (for example, pass the holder or Location , not Point ).
In addition, passing bool as order criteria makes the code less readable. For example, I donβt know what this method does, just looking at it, call DoSomething(list, false) , and I need to see the signature of the method to find out what the semantics of false . It would be much better to use the named parameters DoSomething(list, orderByX : false) (available from C # 4.0 ), but if I don't order X , how do I know that I will then be ordered to Y ? . It also limits the calling code to only two sorting criteria (you would not want to add another sorting flag, would you?)
So, you need to open your intention by making DoSomething name indicating that you are actually ordering your processing. For example TraverseNodesOrderedBy(nodes, point => point.X)
source share