Random array Array.Sort (), Random.range ()

Source: Internet
Author: User
int answerrandom = Random.range (0,4); The tip says it includes the minimum value, not the maximum, which includes 0 excluding 4, and the experimental results are no more than 4 out of 3. int answerRandom1 = Random.range (daan1[0],daan1[3]); A random range can directly use an array of if (TX3). ToString () ==bttona.text) This is converted to the same type using

Long time no research programming. Array.Sort (numbers); Is a quick sort method, by default, from small to large sort

Using System.Collections.Generic; Add this space to use the array List

static void Main (string[] args)
{

int[] numbers = new INT[10];
Random rnd = new Random ();
for (int i = 0; i < i++)
{
Numbers[i] = rnd. Next (100);//We use random numbers to increase the fun of practice
}

Console.WriteLine ("Initialization:");
Display (numbers);
Console.WriteLine ("After sort:");
Array.Sort (numbers);
Display (numbers);

Console.read ();

}

static void Display (int[] numbers)
{
foreach (var item in numbers)
{
Console.Write (item + ",");
}
Console.WriteLine ();

}

The result is an initialization of 17 19 96 98 15 16 25

After sorting, the result is 15 16 17 19 25 96 98

You see, it's easy to sort it out. The default is ascending. So how do you keep him in descending order?

Class intcomparer:icomparer<int>
{

#region Icomparer<int> Members

public int Compare (int x, int y)
{
if (_type = = "DESC")
return Y.compareto (x);//We're just going to turn it around, and we're in a descending order.
Else
return X.compareto (y);
}

#endregion

private string _type = "ASC";
Public Intcomparer (String type)
{
_type = type;
}
}

Then, in the calling code

Console.WriteLine ("Initialization:");
Display (numbers);
Console.WriteLine ("After sort:");
Array.Sort (Numbers, new Intcomparer ("DESC"));

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.