﻿ C# Sharp exercises: Sort the integers in ascending order without moving the number -5 - 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:

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

﻿

New Content: Composer: Dependency manager for PHP, R Programming