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: