Sort int array and intarray

Source: Internet
Author: User

Sort int array and intarray

Today I will learn some basic knowledge about C #, such as sorting the Int Array:

You can create a category, its attributes, and two constructors in the console application:

 

Class Af {private int [] myVar; public int [] MyIntArray {get {return myVar;} set {myVar = value ;}} public Af () {} public Af (int [] arr) {this. myVar = arr ;}}Source Code

 

Next, I will add our data processing methods in this category:
For example, we need to output the result on the screen:



Private void Output (int [] sortResult) {foreach (var I in sortResult) {Console. writeLine (I) ;}} private void Output (IOrderedEnumerable <int> sortResult) {foreach (var I in sortResult) {Console. writeLine (I );}}Source Code


Array sorting, we can use the Sort () method:


Public void SortAsc () {Array. Sort (myVar); Output (myVar );}Source Code

 

Now, we can test the code written above on the console:


What if we need to sort the output in reverse order? You can use the Reverse () method. That is to say, after performing Sort (), you can use the Reverse () method to Reverse the order:

Public void SortDesc () {Array. Sort (myVar); Array. Reverse (myVar); Output (myVar );}Source Code

 
Now, let's go to the console to see how the code is written:



Here, the original function has been implemented, but Insus. NET wants to use another method to implement the same function:


Public void ArrayOrderBy () {var result = myVar. orderBy (g => g); Output (result);} public void ArrayOrderByDescending () {var result = myVar. orderByDescending (g => g); Output (result );}Source Code

 

Running result:

 

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.