A HashMap using a String (Or even a character) as the key (Letter of the alphabet) and a array as the value.
Looks like a website, probably an overview.
The answer is: templates and php
I think you mean something like this:
items = sorted(file("items.txt").readlines())
current = ""
clines = 0
maxlines = 50
print "<table><tr><td>"
for i in items:
if i[0].upper() != current:
current = i[0].upper()
print "<h1>", current, "</h1>"
clines += 1
if clines >= maxlines:
print "</td></td>"
clines = 0
print i
print "</td></tr></table>"
Surely the HTML sucks, but that's the idea, when you exceed the maximum lines per column, switch to the next column.
If you want to make sure that a column always starts with a heading, then you should switch to the next column when the current number of lines plus the size of the next group exceeds the maximum number of lines per column.
I would probably approach it in the following manner:
- If the input list is unsorted, find a technique (LINQ, etc.) to group and sort your item groups and items. That doesn't really seem to be your question here, although I'm open to correction - there are dozens of techniques for doing that, so it should be a popular question on its own...
- Identify the number of desired columns. We could even try different numbers of columns later in the algorithm, to try to get "best-fit".
- Divide the total number of detail items (possibly including the headers as well - I'm designing this on my feet...) by the number of columns. This will be the approximate number of items we want in each column.
- Starting from the left, place items into the column until adding another group would run over the number of items per column. (probably need to consider the possibility of one freakishly-large group that fills an entire column by itself...) At this point, we need to decide whether the next group goes in this column or the next one. I'd suggest something like this: if the break point within this group is in the upper half - i.e. more items in the group would be below the desired cut-off line than above it - go to the next column. Otherwise, add the group to the current column. Obviously if we are in the last column, there is no "next" column so we just add it to this one.
The hope is that using this technique - allowing each column to overflow slightly - will balance out over time and space. Otherwise, we just keep pushing groups to the next column and our final column ends up significantly longer than the others.