Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Sort the integers in ascending order without moving the number -5

C# Sharp Basic: Exercise-61 with Solution

Write a C# program to sort the integers in ascending order without moving the number -5.

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;
using System.Collections;

public class Example
    {
      public static int[] sort_numbers(int[] arra)
        {
     
            int[] num = arra.Where(x => x != -5).OrderBy(x => x).ToArray();
            int ctr = 0;

            return arra.Select(x => x >= 0 ? num[ctr++] : -5).ToArray();
        }
  
   public static void  Main()
        {
           int[] x = sort_numbers(new int[] {-5, 236, 120, 70, -5, -5, 698, 280 });
            foreach(var item in x)
             {
               Console.WriteLine(item.ToString());
              }
        }        
}

Sample Output:

-5
70
120
236
-5
-5
280
698

Flowchart:

Flowchart: C# Sharp Exercises - Sort the integers in ascending order without moving the number -5

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to calculate the sum of all the intgers of a rectangular matrix except those integers which are located below an intger of value 0.
Next: Write a C# program to reverse the strings contained in each pair of matching parentheses in a given string and also remove the parentheses within the given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.