I have list of integer and I am trying to sort this based on first in descending order and after that I want to sort with in two list based on even and odds but still in descending order like this
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var numbers = new List<int>() { 3, 7, 1, 5, 4, 6, 2 };
var sortedDescendingNumbers = numbers.OrderByDescending(x => x);
var sortedNumbers = sortedDescendingNumbers.ThenByDescending(x => x % 2 == 0);
foreach (var num in sortedNumbers)
{
Console.Write(num + " ");
}
}
}
but this one print the result as 7 6 5 4 3 2 1 and I am expecting output like 7 5 3 1 6 4 2
>Solution :
Your first block of numbers should be the odd ones, so you have to order by divisibility by 2 first and then by descending value.
var sortedOddEvenNumbers = numbers.OrderBy(x => x % 2 == 0);
var sortedNumbers = sortedOddEvenNumbers.ThenByDescending(x => x);