Array. Sort () Static Method to Sort Arrays

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.