For sorting, sorting is found to be used in many places in the project. It is very tiring to write forward, backward, and backward orders. However, it is good to write a comprehensive method for sorting.
1. Obtain the data of XX.
Ienumerable <XXXX >= .....................;
2. Get the orderby parameter to be sorted.
Orderby parameter format:
Orderby = "createtime ASC, title DESC ";
--------------------------------------------------------------------------
Orderusingsortexpression (res. asqueryable (), orderby). asenumerable ();
3. sorting data
Public static iorderedqueryable <tentity> orderusingsortexpression <tentity> (this iqueryable <tentity> source, string sortexpression) Where tentity: Class
{
String [] orderfields = sortexpression. Split (',');
Iorderedqueryable <tentity> result = NULL;
For (INT currentfieldindex = 0; currentfieldindex <orderfields. length; currentfieldindex ++)
{
String [] expressionpart = orderfields [currentfieldindex]. Trim (). Split ('');
String sortfield = expressionpart [0];
Boolean sortdescending = (expressionpart. Length = 2) & (expressionpart [1]. Equals ("DESC", stringcomparison. ordinalignorecase ));
If (sortdescending)
{
Result = currentfieldindex = 0? Source. orderbydescending (sortfield): result. thenbydescending (sortfield );
}
Else
{
Result = currentfieldindex = 0? Source. orderby (sortfield): result. thenby (sortfield );
}
}
Return result;
}
4. In this way, no matter how many backend methods are sorted, only one orderby parameter is defined, which is fine according to certain specifications.