In c#, how to sort list of doubles by mantissa?

You can achieve this through OrderBy() and Math.Truncate() method as like the following. Where x-Math.Truncate(x) gives you the number after the decimal point and OrderBy will arrange them in the ascending order. Have a look at this example and try yourself with the following snippet

 List<double> input = new List<double>(){1.2, 2.3, 1.12, 5.1};
 input = input.OrderBy(x=>x-Math.Truncate(x)).ToList();
 Console.WriteLine(String.Join("\n",input));

Or you can try this as well .OrderBy(x=>x-(int)x) instead for OrderBy(x=>x-Math.Truncate(x)

Tags:

C#

.Net

Linq