Public classStudentcardsorthelper<t>{//itemname The name of the field to sort Public StaticList<studentcard> Sortbycarditem (stringItemName, SortOrder SortOrder, list<studentcard>listitemdata) {List<KeyValueData<T>> SD =NewList<keyvaluedata<t>>(); List<StudentCard> returnlist =NewList<studentcard>(); for(inti =0; i < Listitemdata.count; i++) { varv = listitemdata[i]. Studentcarditems.find (c =c.name.equals (itemname)); if(typeof(T) = =typeof(int)) { Objectobj = String.IsNullOrEmpty (v.text)?int. MinValue:Convert.ToInt32 (V.text); Sd. ADD (NewKeyvaluedata<t>((T) obj, listitemdata[i])); } Else { Objectobj =V.text; Sd. ADD (NewKeyvaluedata<t>((T) obj, listitemdata[i])); } } if(SortOrder = = sortorder.ascending)//Ascending{List<KeyValueData<T>> list = sd. (c = c.key). Tolist<keyvaluedata<t>>() ; foreach(varIteminchlist) {Returnlist.add (item. Studentcard); } } Else//Descending{List<KeyValueData<T>> list = sd. OrderByDescending (c = c.key). Tolist<keyvaluedata<t>>(); foreach(varIteminchlist) {Returnlist.add (item. Studentcard); }} listitemdata=returnlist; returnListitemdata; } //key can be repeated Public classKeyvaluedata<t> { PublicT Key; PublicStudentcard Studentcard; PublicKeyvaluedata (T key, Studentcard studentcard) { This. Key =key; This. Studentcard =Studentcard; } }}
Using the method, where the original data of the different columns is of type string, but the age of the sort directly by string will be problematic, so the column type is automatically judged, Listitemdata is the list of student cards before sorting.
if (V.itemdatatype = = datatype.type_number) {= studentcardsorthelper<int>. Sortbycarditem (ItemName, sortorder,listitemdata);} Else { = studentcardsorthelper<string>. Sortbycarditem (Itemname,sortorder, listitemdata);}
The following are the individual type definitions
Public enum datatype{ type_string, type_number}
// Data List Public New List<studentcard> ();
Public class carditem{ private String name; name private String text; Show text private DataType itemdatatype; Data type
{.. Getset..}
}
The number of fields is dynamic.
Public class studentcard{ publicgetsetnew list<carditem>();}
Dynamic field List implementation and list<t> sorting