This example defines an int Array, uses the Array. Sort (arr) Static Method to Sort the Array, and finally outputs the sorted Array. In the preceding example, output 1, 2, 3, 4, 5, 6 in sequence.
Int [] intArray = new int [] {2, 3, 6, 1, 4, 5 };
Array. Sort (intArray );
Array. ForEach <int> (intArray, (I) => Console. WriteLine (I ));
Sorting is one of the commonly used methods in programming. There are many sorting methods. The following describes a simple and effective sorting method. The Code is as follows:
Private bool isReverse = false;
Private void Sort (personalnotifentity [] list, string key)
{
If (isReverse)
{
Array. Reverse (list );
IsReverse = false;
}
Else
{
Int len = list. Length;
Type type = typeof (PersonalNotificationEntity );
Object [] keys = new object [len];
For (int I = 0; I <len; I ++)
{
Keys [I] = type. InvokeMember (key, BindingFlags. GetField, null, list [I], null );
}
Array. Sort (keys, list );
IsReverse = true;
}
}
The Array. Sort () and Array. Reverse () methods are used to Sort and Reverse the data. The isReverse variable is used as the marker of Reverse sorting.
Two parameters are passed in. One is the list of objects to be sorted, and the other is the key of the sort keyword, that is, the object is sorted Based on the attribute or field (the value is equal to the attribute/field name of the object)
The type. InvokeMember () method can obtain the property/Field Value of the object instance.