Recursive selection sort in C#

A C# version of the selection sort inspired by the way it’s commonly done in Scala


Insertion sort in C#

Following the same theme as the last two posts, here is a Scala style implementation of the insertion sort in C#

The code for this is way simpler than I expected. The bubble sort is supposed to be one of the simplest sorting algorithms, yet the code is a lot more involved than this insertion sort code.

An attempt to merge sort functionally in C#

In a month I’ll be starting a new job which will be focused on big data using Scala and Apache Spark.
In preparation, I’ve been re-familiarising myself with Scala by implementing various of the most common data structures and algorithms in Scala.

When comparing how C# (my current occupation programming language) programmers and Scala programmers implement the merge sort, I noticed they are usually quite different in approach. This is partly due to C# and Scala implementing their list data type very differently.
So to help me with my understanding of the merge sort, I decided to try and write it in C# in a way that people generally implement it in Scala.

This github link shows what I came up with.

I think this has helped my understanding of the logic and implementation of the merge sort, so I may continue doing this with other algorithms. As that forces me to really understand what is going on instead of memorising the code.

Join two tables based on value only present in one

I needed to join two tables where one is a list of orders, and the other is a list of items in the orders.
These items can only be for orders created by a specific customer.

The orders table has a foreign key via the customer ID, but the items do not.

You could get all the items, and then load it into memory and filter based on the order’s customer ID value, but doing this uses up a lot of memory and is very slow. I needed to do this as lazy as possible.

Fortunately in LINQ, you can join based on anonymous objects that you can create using the table values and other values not in the table. And as it’s all done via IQueryables, so the execution is deferred as late as possible.
int customerID = 12345;

return from item in context.items
join order in context.Orders
on new { orderID = item.OrderID, customerID = customerID } equals new { orderID = order.ID, customerID = order.CustomerID }
select item;

So even though there is no customer ID column in the items table, the anonymous object I created has one that is set to the same value as the customerID I’m looking for. So as long as the customer ID value in an order row matches that, I will get the items I want.

Chaining function calls using the result of the previous function

I found myself wanting to apply a set of functions which would return a modified version of the passed in string, to which I wanted to apply the result to the next function. This led to me writing code like so:

val modifiedContent1 = repeatedHeaderRegex.replaceAllIn(content, “”)
val modifiedContent2 = repeatedMultipleWordsWithDateHeaderRegex.replaceAllIn(modifiedContent1, “”)
repeatedSeparatedByBulletPointHeaderRegex.replaceAllIn(modifiedContent2, “”)

Which is pretty ugly and would mean needing to add more code for any additional regexes.

After digging around for some kind of function that would make this less ugly\verbose, I came across the foldLeft function.
This allowed me to pass in the original content string and then apply the current regex item’s findAllIn function to the result of the previous iteration. This does exactly the same as the above code, but looks like so:

Seq(repeatedHeaderRegex, repeatedMultipleWordsWithDateHeaderRegex, repeatedSeparatedByBulletPointHeaderRegex)
.foldLeft(content)((text, regex) => regex.replaceAllIn(text, “”))

This is good to keep in mind in the future as I do need to do something like this fairly often, but I would just do separate functions for each case instead of putting them all in one like I did in the first code block.
I looked for an equivalent in C# and came across the Aggregate function, so I’m glad I can do this there too.

A reason why TryUpdateModel or UpdateModel can pass yet not correctly update the model’s properties.

I just spent over an hour wondering why a post controller function’s call to UpdateModel was not updating the view model’s properties with the values in the form collection.

The mysterious thing was that UpdateModel was not throwing an exception which I as the caller would expect if it does not actually update the properties.

It turns out that the view model had been refactored a tiny bit recently and the relevant properties had their setter made private.
Usually when something wants to modify a property with a private set, a compiler error will appear informing you about this. This was not the case here sadly.

So as a reminder to myself,

When UpdateModel or TryUpdateModel does not update correctly, first thing to do is check that the relevant view model’s properties setters are public.