Hi there
I am wondering how to best solve a list sorting problem.
Mentioned list initially consists of recipients ordered by First + Last Name. A given number of recipients fit on a report sheet to be printed.
Now, the report sheets are stacked upon each other and then cut vertically. The entire list of recipients must be rearranged in a sort of way that each resulting pile of cuttings is contiguously ordered by First + Last Name..
This is a working solution I came up with – but it isn’t very efficient.
I clone the list and iterate the clone multiple times to bypass recipients of previous pages and to collect every n-th recipient for the current page:
public void RearrangeRecipients(byte recipientsPerSheet)
{
// Clone and clear all food card recipients
var clonedRecipients = new List<IFoodCardRecipient>(recipientsList);
recipientsList.Clear();
// Determine sheet count
var sheetCount = Math.Ceiling((double)clonedRecipients.Count / recipientsPerSheet);
for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++)
{
// Bypass recipients of previous pages, then take every n-th recipient for the current page
var recipientsCurrentSheet = clonedRecipients.Skip(sheetIndex).Where((recipient, index) => index % sheetCount == 0);
recipientsList.AddRange(recipientsCurrentSheet);
}
}
Thanks for any tipps :)