Sort a list by columns but iterate by rows

February 24th, 2019

If you have a list of objects, lets say of strings ordered alphanumerically; “A”, “B”, “C”, “D” and you want to sort them by columns like the following, yet print them out by rows.
A C
B D

Here is a refined, rows-iterable, version of a previous method I blogged here: Sorting columns using a list of objects (c#).

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System.Collections.Generic;
 
/// <summary>
/// Used to sort a list of objects into columns, ready to
/// render by iterating each row of the resulting array of lists.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sourceList">The source list, sorted as the desired output</param>
/// <param name="columns">The desired number of columns to sort the list by</param>
/// <returns>An array with lists of T sorted by columns but grouped in rows</returns>
public List<T>[] SortByColumns<T>(List<T> sourceList, int columns) {
	if(sourceList == null || sourceList.Count == 0 || columns < 2)
		return null;
 
	var total = sourceList.Count;
	var columnLimit = (total + (columns - 1)) / columns; // better than Ceiling((double)total / columns)
 
	var targetList = new List<T>[columnLimit];
	for(var c = 0; c < targetList.Length; c++) {
		targetList[c] = new List<T>();
	}
 
	for(var i = 0; i < total;) {
		foreach(var row in targetList) {
			row.Add(i < total ? sourceList[i++] : default(T));
		}
	}
 
	return targetList;
}

Solution: Make an entire table row clickable

April 27th, 2014

To enable an entire row clickable with a specific link in a table has been a recurring problem for web developers. There are some solutions on the web, but they all seem to be lacking in some way or another. Here is my solution to link an entire row in a table, including working context menu and control click to open in new tab.
Read the rest of this entry »

How to set up logging with NLog in a .NET project

March 1st, 2014

Logging is quite useful, and one popular tool for .Net logging is log4net. Another really nice tool is NLog. Here’s how to implement logging and configure NLog for your website in a few steps. Read the rest of this entry »

AJAX: Javascript templating for updating html with JSON response

February 9th, 2014

Updating a web page with new data from a response after an AJAX call is a very common task, but still many developers send entire html chunks in the response to the javascript, probably mostly due to laziness, while others send JSON but have prepared html in the javascript file which isn’t much better and could be a hassle to modify at a later stage.
Read the rest of this entry »

AJAX: Bring attention to an updated element

February 9th, 2014

Sometimes when you’ve updated existing content on the users screen via AJAX you might want the updated part
flash or highlight in some way to illustrate what part of the page was just updated.
There might be other reasons to bring attention to a certain area of a page too, possibly a missing value, notification etc.  Read the rest of this entry »

Yet another… How to upgrade to EPiServer 7.5

February 9th, 2014

I struggled with the upgrading from EPiServer CMS 7.1 to EPiServer 7.5, even if there are several good blog posts about this already (Ted & Gustaf, David Vujic) it seems everyone comes up with new obstacles so I thought I’d share my step-by-step guide.  Read the rest of this entry »

How to add an ajax loader to your website

January 22nd, 2014

We all love AJAX calls, but it’s important to show the visitors that the page is loading.
Here’s an easy way of implementing a generic AJAX loader image to your website which is shown for all ajax calls.
Read the rest of this entry »

Sorting columns using a list of objects (c#)

September 17th, 2013

Rendering a collection of items in columns could be problematic, at least if you want to sort the items vertically in each column, or you need to use separate surrounding html tags for some reason.

Example: You have a list of letters – a, b, c, d, e, f – and you want to group them like this:
a   c   e
b   d   f

Read the rest of this entry »

Arrow image after external links – CSS tweak

September 17th, 2013

Here’s a little CSS tweak to have a custom image, like this shown directly after any external links in a list. By external, I mean they begin with http even though you could argue that internal links with a full absolute URL also could begin with that as well.
Read the rest of this entry »

The type arguments for method cannot be inferred from the usage

August 19th, 2013

Visual Studio can have problems understanding LINQ expressions in Razor views, complaining with the recurring error message when hovering the inline expression:
“The type arguments for method … cannot be inferred from the usage. Try specifying the type arguments explicitly.”

Even though the code is correct and everything works as expected, the MVC view gets cludded with warnings and Intellisense doesn’t work inside the LINQ expressions.

Solution:
Add targetFramework="4.0" in the compilation node in web.config, and then re-compile:

1
<compilation defaultLanguage="c#" targetFramework="4.0">