# C# Soft Number By Difference From Input

I have the following code. Please help me get my desired output. I have a list of numbers then I have input value which is 4. I want that my list will be sorted based on 0 difference first then the rest will be ascending. For example in my list there are 4s. My input is 4 so I want to sort those numbers where number item – input value=0 (4-4=0).

C#

```    static void Main(string[] args)
{
var numbers = new List<int> { 1, 2, 3, 4, 4, 5, 6, 7, 4, 8, 1, 4 };
var sortedNumbers = numbers.OrderBy(x => x - 4 == 0);
foreach (var item in sortedNumbers)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
```

OUTPUT

```1
2
3
5
6
7
8
1
4
4
4
4
```

Desired Output

```4
4
4
4
1
1
2
3
5
6
7
8
```

## Answer

You can achieve your desired output by using a ternary expression inside the `OrderBy` lambda :

```    static void Main(string[] args)
{
var numbers = new List<int> { 1, 2, 3, 4, 4, 5, 6, 7, 4, 8, 1, 4 };
var sortedNumbers = numbers.OrderBy(x => x==4? int.MinValue: x);
foreach (var item in sortedNumbers)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
```

output :

```4
4
4
4
1
1
2
3
5
6
7
8
```

Edit : in case you have zero or negative number in your list, your number will be order as follow : first, exact match, then ascending. For example : `{ -1, 0, 0, -12, 1, 2, 3, 4, 4, 5, 6, 7, 4, 8, 1, 4 }`

the result will be the following :

```4
4
4
4
-12
-1
0
0
1
1
2
3
5
6
7
8
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.5589725,"math_prob":0.9593976,"size":1325,"snap":"2021-04-2021-17","text_gpt3_token_len":485,"char_repetition_ratio":0.11809235,"word_repetition_ratio":0.33687943,"special_character_ratio":0.39698112,"punctuation_ratio":0.20231214,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95864254,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-15T13:17:52Z\",\"WARC-Record-ID\":\"<urn:uuid:0ca3414b-887c-426a-a3c7-5207d6ee41a5>\",\"Content-Length\":\"78784\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b366e1e0-70ac-4803-a13b-657f04e0123d>\",\"WARC-Concurrent-To\":\"<urn:uuid:303231b5-5699-4314-b083-8f856cc38743>\",\"WARC-IP-Address\":\"172.67.151.92\",\"WARC-Target-URI\":\"https://www.tutorialguruji.com/c-sharp/c-soft-number-by-difference-from-input/\",\"WARC-Payload-Digest\":\"sha1:EE6IUIBVBOMFCCNORTUSVVC27NOKIFVM\",\"WARC-Block-Digest\":\"sha1:4D2RSFPG7HBW5JHI7UYCYXOLKYXIIM72\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038085599.55_warc_CC-MAIN-20210415125840-20210415155840-00173.warc.gz\"}"}