List of alphabet sort to display vertically

1.1k views Asked by At

I have a list, containing the alphabet.

When outputting with foreach, i will currently receive this result:

  • A B C
  • D E F
  • G H I

What i would like to achieve is this

  • A D G
  • B E H
  • C F I

Ive tried the following, but really, i doesent find it a good or stable solution - and the letters/indexes are presented twice.

        @{
            var ie = 0;
            int sortI = 0;

}
            int columns = 3;
            int dictLength = ToShow.Count();
            int rows = dictLength/columns;

            int sortS = 1;
            List<int> hasKey = new List<int>();

            var newDict = new Dictionary<string, List<LoopItem> >();


            if( sort.Equals("alphabetically") )
            {

                while( sortI <= rows )
                {

                        <text>Column @( ToShow.Keys.ElementAt(sortI) ) - </text>

                        while( sortS <= ( columns ) )
                        {           
                                int index = (( sortS *  columns ) + sortI); //(( sortS *  columns ) + ( 1 + sortI ));

                                if( hasKey.Contains(index) ) { continue; } else
                                {

                                    <text>Column @( ToShow.Keys.ElementAt(index) ) - </text>
                                    sortS++;
                                    hasKey.Add(index);
                                }
                        }
                            <text><br /></text>
                        sortS = 1;
                        sortI++;
                }

                sortS = 1;
                sortI = 1;
                <text><hr /></text>
                foreach( var item in ToShow )
                {
                        <text><br/>::Række 1::<br /></text>
                    while( columns >= sortS )
                    {
                            <text>Indsæt @(sortI+sortS)<br /></text>
                            sortS++;
                    }

                    sortI++;
                    sortS = 1;
                }

            }

        }

Are there any algorithm for this kind of stuff? Or a simple function?

Thanks in advance

3

There are 3 answers

1
David On BEST ANSWER

If you know how many letters you want to present on each row you could do something like this:

var alphabet = new []{'A', 'B', 'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
var rows = 3;
var alphabetIndex = 0;

for(var row = 0; row < rows; row++) {
    for(var letter = alphabetIndex; letter < 26; letter += rows) {
        Console.Out.Write(alphabet[letter] + " ");
        if(letter + rows >= 26)
            alphabetIndex = (letter + rows) - 26;
    }
    Console.Out.WriteLine();
}

Rows will set how many rows you want, other than that it should work out for you.

0
NicoE On

It really applies to being able to "vertically" group and sort any (IComparable) collection, a series of characters such as the alphabet probably being the most visible/common one.

Depending on the requirement, either number of rows (Bob's answer) or number of items per column:

public static IList<IList<T>> groupCollection<T>(List<T> comparable, int groupSize, bool sortAscending = true) where T : IComparable
{
    var groups = new List<IList<T>>();
    if (comparable != null && groupSize > 0)
    {
        var items = sortAscending ? comparable.OrderBy(item => item).ToList() : comparable.OrderByDescending(item => item).ToList();
        int totalItems = comparable.Count;

        int totalGroups = totalItems % groupSize > 0 ? totalItems / groupSize + 1 : totalItems / groupSize;
        for (int groupIndex = 0; groupIndex < totalGroups; groupIndex++)
        {
            int k = 0;
            for (int j = groupIndex; j < totalItems && k < groupSize; j += totalGroups)
            {
                k++;
                if (groups.ElementAtOrDefault(groupIndex) == null)
                {
                    groups.Add(new List<T>());
                }
                groups[groupIndex].Add(items[j]);
            }
        }
    }
    return groups;
}
0
Andrea Scarcella On

Quick Example with LINQ:

 void Main()
{
     var targetNrOfRows = 3;
        var alphabet = new[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

        var r = alphabet
        .Select((el, i) => new { row = i / targetNrOfRows, col = i % targetNrOfRows, el2 = el })//compute indices of matrix 'A B C' and so forth
        .Select(el => new { row2 = el.col, col2 = el.row, el3 = el.el2 })//Transpose said Matrix and thus obtain the desired matrix
        .OrderBy(el => el.row2).ThenBy(el => el.col2);//Order by row and then by column

        for (int i = 0; i < targetNrOfRows; i++)//print it out
        {
            var row = r.Where(el => el.row2 == i).Select(el => el.el3);//get i-th row
            foreach (var item in row)
            {
                Console.Write(item); 
            }
            Console.WriteLine();
        }
r.Dump();
}

Let me know whether you have any questions.