Normally, if you knew the property with which you wanted to sort, you would be able to just do something like this:
clients.Sort(Function(x, y) x.Name.CompareTo(y.Name))
In the above example, I am, of course sorting on the Name property (I don't know what properties the Client class has, I'm just using it as an example.
However, since you don't know which property you are going to use until run-time, you'll need to do something more complicated. If you really want to use the actual property names of the class, you could use Reflection to retrieve the value of the property dynamically, for instance:
clients.Sort(Function(x, y)
Dim xProperty As PropertyInfo = x.GetType().GetProperty(ViewState("PropertyName").ToString)
Dim yProperty As PropertyInfo = y.GetType().GetProperty(ViewState("PropertyName").ToString)
Dim xValue As Object = xProperty.GetValue(x)
Dim yValue As Object = yProperty.GetValue(y)
Return xValue.ToString().CompareTo(yValue.ToString())
End Function)
To reverse the sort order, just multiply the return value by -1, or switch which object you are comparing to what. For instance:
If ViewState("Order") = "Ascending" Then
Return xValue.ToString().CompareTo(yValue.ToString())
Else
Return yValue.ToString().CompareTo(xValue.ToString())
End If
DataTableinstead where you can use the column-name and LINQ to order it.