0

This specification requires the algorithm to have the lowest possible order of complexity. I am trying to create my own version of LINQ .OrderBy() to achieve this, using a Radix Sort.

IE I am trying to create

var sortedListOfAppointments = listOfAppointments.OrderByDateUsingRadixSort(a => a.AppointmentDateTime);

I currently have a method which accepts any IEnumerable list, and sorts by the stated date field, but at the moment, it only returns the sorted Dates, not the sorted list.

To start with, I have a method which Sorts dates as follows:-

public static IEnumerable<DateTime> SortDatesUsingRadixSort(this IEnumerable<DateTime> dates)

Then I have a method to accept any class, and use the stated field as the field to sort by

public static IEnumerable<DateTime> OrderByDateUsingRadixSort<TSource>(this IEnumerable<TSource> dates, Func<TSource, DateTime> selector) =>
    (from date in dates select selector(date)).SortDatesUsingRadixSort();

This works, returns a list of sorted dates, and can be used as follows:-

var sortedDates = listOfAppointments.OrderByDateUsingRadixSort(a => a.AppointmentDateTime);

But I am wanting

var sortedListOfAppointments = listOfAppointments.OrderByDateUsingRadixSort(a => a.AppointmentDateTime);

IE, I am wanting OrderByDateUsingRadixSort to return IEnumerable, but what needs to change in this method to achieve it?

public static IEnumerable<TSource> OrderByDateUsingRadixSort<TSource>(this IEnumerable<TSource> dates, Func<TSource, DateTime> selector) =>
    // what needs to change to return the whole list sorted?       (from date in dates select selector(date)).SortDatesUsingRadixSort();

I feel like I am missing something small but fundamental in my understanding here.

Sources used to get this far:- https://www.geeksforgeeks.org/how-to-efficiently-sort-a-big-list-dates-in-20s/ https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-add-custom-methods-for-linq-queries

Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
delphie
  • 1
  • 1
  • You have to sort not List of Dates but List of classes which contains date. – Svyatoslav Danyliv Mar 28 '23 at 10:40
  • I am pretty sure `OrderBy` is very efficient (optimized quicksort). LINQ methods should return an object that represents the operation (e.g. sort) until enumerated. You won't be able to use your sort dates method to implement the orderby method - you will need to rework the code of the method to sort the original objects. – NetMage Mar 28 '23 at 14:44

0 Answers0