-5

I have an array like this

int[] intnumber = new int[]{10,25,12,36,100,54,68,75,63,24,1,6,9,5};

I want to find the greatest number and make it In order from largest to smallest

like this 100,75,68,63,54,36,25,24,12,10,9,6,5,1

  • 4
    Possible duplicate of [C# find highest array value and index](http://stackoverflow.com/questions/13755007/c-sharp-find-highest-array-value-and-index) – fubo Aug 11 '16 at 06:03
  • 5
    Possible duplicate of [Better way to sort array in descending order](http://stackoverflow.com/questions/5430016/better-way-to-sort-array-in-descending-order) – Raskayu Aug 11 '16 at 06:05
  • So basically you want to sort descending. Your title should say that. And I cannot believe that you didn't find any helpful resources for that. – Steve Aug 11 '16 at 06:08
  • I can find Greatest Number but I can't make it In order from largest Number to smallest Number – MApplications Aug 11 '16 at 06:12
  • I don't have time to search in the net for the answer and as you know when you ask question here ,people will help you as soon as they can – MApplications Aug 11 '16 at 06:36
  • but I don't know why always my score is -2 or -3 or -4 – MApplications Aug 11 '16 at 06:38
  • 2
    Your score is so bad because people here have better things to do than help ungrateful lazy people that can't be bothered to use the search function of SO. – Mike Scotty Aug 11 '16 at 06:50

7 Answers7

1
int[] intnumber = new int[] { 10, 25, 12, 36, 100, 54, 68, 75, 63, 24, 1, 6, 9, 5 };
int maxValue = intnumber.Max();

You can sort the array for viewing elements in ascending order

Array.Sort(intnumber);
Array.Reverse(intnumber);
foreach (var str in intnumber )
{
    MessageBox.Show(str.ToString());
}
vivek kv
  • 406
  • 6
  • 11
  • *find the greatest number and make it In order from largest to smallest* So i hope he is looking for a sorting in descending, It will be better if you Include that too – sujith karivelil Aug 11 '16 at 06:08
0
 exactly output that you want.

   int[] intnumber = new int[] { 10,25,12,36,100,54,68,75,63,24,1,6,9,5 };
            Array.Sort<int>(intnumber ,
                            new Comparison<int>(
                                    (i1, i2) => i2.CompareTo(i1)
                            ));
                        intnumber .Dump();

P.S. To run this demo you need to follow these steps:

1.Download LINQPad.

2.Download the demo file, open it with LINQPad and hit F5.

KARAN
  • 1,023
  • 1
  • 12
  • 24
0

You can use :

int[] intnumber = new int[]{10,25,12,36,100,54,68,75,63,24,1,6,9,5};

Array.Sort(intnumber );

Array.Reverse(intnumber );

int max = intnumber[0];
Prabhat Sinha
  • 1,500
  • 20
  • 32
Amit Sinha
  • 566
  • 7
  • 22
0

Try this,

        int[] intnumber = new int[] { 10, 25, 12, 36, 100, 54, 68, 75, 63, 24, 1, 6, 9, 5 };
        //Maximum Value
        int maxValue = intnumber.Max();
        //Maximum Index
        int maxIndex = intnumber.ToList().IndexOf(maxValue);
Prabhat Sinha
  • 1,500
  • 20
  • 32
0

I found my answer with your helps

            Console.WriteLine("How many Numbers Do you want? ");
        int counter = int.Parse(Console.ReadLine());
        double[] numbers = new double[counter];
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.Write((i + 1) + " : ");
            numbers[i] = Convert.ToDouble(Console.ReadLine());
        }
        Console.WriteLine("_______________________________________________");
        Array.Sort(numbers);
        Array.Reverse(numbers);
        foreach (double item in numbers)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine("_______________________________________________");
        Console.WriteLine("The Greatest Number is " + numbers[0]);
        Console.ReadKey();
0

Let intNumbers be the array that you are using, Then you can use the .Max() method of the Array Class to get the maximum value, that is the greatest number. If you want to Sort the Current array means You have to use the .Sort() method. The requirement is simply Printing the Array in descending order means you have to use the .OrderBy()

int[] inputNumbers = new int[] { 15, 12, 11, 23, 45, 21, 2, 6, 85, 1 };
Console.WriteLine("Input Array is              : {0}\n",String.Join(",",inputNumbers.OrderByDescending(x=>x)));
Console.WriteLine("Max value in the array is   : {0}\n",inputNumbers.Max());
Console.WriteLine("Array in descending order   : {0}\n",String.Join(",",inputNumbers.OrderByDescending(x=>x)));

Here is a working Example

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
-1
int max = Integer.MIN_VALUE;

for (int i =0;  i < intnumber.length; i++)
{    
   int num = intnumber[i];

   //Check to see if num > max.   If yes, then max = num.   
}

System.out.println(max);
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
MongooseLover
  • 95
  • 1
  • 11