I have to iterate data through some DIV's. I don't know how to share and debug the code with others, so I decided to put the code in C# in a console application if you don't mind.
Problem is that I cannot close section div in this iteration. If you have a better idea, please, share. I'm stuck.
Thank you.

string[] someWords = { "auto", "botox", "amater", "test_phase", "warengruppen", "axel" };
            List<char> letters = new List<char>();
            Array.Sort(someWords);
            someWords.ToList();
            //fill b list with alphabet 
            foreach (var characters in someWords) {
                if (!letters.Contains(characters[0])) {
                    letters.Add(characters[0]);
                }
            }
            letters.Sort();//make it asc
            int i = 0;
            string substitute = "";
            string output = "";

            output += "\n<div class='wrap'>";

            while (i < someWords.Count()) {
                foreach (var c in letters) {
                    if (someWords[i].StartsWith(Convert.ToString(c))) {
                        if (substitute != Convert.ToString(c)) { 
                            substitute = Convert.ToString(c);
                            output += "\n<div class='section_" + substitute + "'>";
                        }
                        output += "\n<div class='container'>";
                        output += "\n" + someWords[i];
                        output += "\n</div>";//end of container div
                        substitute = Convert.ToString(c);
                    }
                    output += "\n</div>";  //end of section div
                }
                i++;
            }

            output += "\n</div>";//end of wrap div
            Console.WriteLine(output);

The result I would like to have:

<div class='wrap'>

    <div class='section_a'>
        <div class='container'>
        amater
        </div>
        <div class='container'>
        auto
        </div>
        <div class='container'>
        axel
        </div>
    </div> //end of section_a

    <div class='section_b'>
        <div class='container'>
        botox
        </div>
    </div> //end of section_b

    <div class='section_t'>
        <div class='container'>
        test_phase
        </div>
    </div> //end of section_t

    <div class='section_w'>
        <div class='container'>
        warengruppen
        </div>
    </div> //end of section_w

</div> //end of wrap div

Recommended Answers

All 3 Replies

As for your immediate problem it looks like your logic is a bit wrong. If you loop through the characters and add the words starting with that character your code will work much better.

A few things to note:

When concatenating strings continuously like this use a StringBuilder. This way each concatenation doesn't create a new string.

When mixing strings and objects use the concatenation operator($) this really cleans up your code.

Since you have to keep converting the characters to strings, you can use the SubStringmethod and just keep them as strings instead of using the index and just getting a character.

This line:

    someWords.ToList();

doesn't do anything unless you assign it to a new variable.

With this in mind your code could look like this:

    string[] someWords = { "auto", "botox", "amater", "test_phase", "warengruppen", "axel" };
    List<string> letters = new List<string>();
    Array.Sort(someWords);
    //fill b list with alphabet 
    foreach (var characters in someWords)
    {
        string firstLetter = characters.Substring(0, 1);
        if (!letters.Contains(firstLetter))
        {
            letters.Add(firstLetter);
        }
    }
    letters.Sort();//make it asc
    StringBuilder output = new StringBuilder("\n<div class='wrap'>\n");
    foreach (var c in letters)
    {
        output.AppendLine($"<div class='section_{c}'>");
        while (i < someWords.Length && someWords[i].StartsWith(c))
        {
            output.AppendLine($"<div class='container'>");
            output.AppendLine(someWords[i]);
            output.AppendLine("</div>");
            i++;
            if (i == someWords.Length)
            {
                break;
            }
        }
        output.AppendLine("</div>");
    }
    output.AppendLine("</div>");

This having been said. Your code is inefficient. You don't need a list of beginning characters. You can use 2 nested loops with the same iterator variable to grab the first letter each time it changes. Your code when optimized like this becomes much leaner:

    string[] someWords = { "auto", "botox", "amater", "test_phase", "warengruppen", "axel" };
    Array.Sort(someWords);
    StringBuilder output = new StringBuilder("\n<div class='wrap'>\n");
    int i = 0;
    while(i < someWords.Length)
    {
        string firstLetter = someWords[i].Substring(0, 1);
        output.AppendLine($"<div class='section_{firstLetter}'>");
        while( i < someWords.Length && someWords[i].StartsWith(firstLetter))
        {
            output.AppendLine($"<div class='container'>");
            output.AppendLine(someWords[i]);
            output.AppendLine("</div>");
            i++;
        }
        output.AppendLine("</div>");
    }
    output.AppendLine("</div>");
    Console.WriteLine(output);

Thank you!!
Code is much more efficient and better!
How to achieve better coding like yours? Even when I am aware of this problem but mine logic still tends to complicate things.
If you have a clue, share :)

Thank you one more time

One trick that is really worth learning, is to reduce a problem down to its bare minimum. Also the principle of singularity is very important, every class/method should concern itself with one thing/problem. In short keep it simple silly, the KISS principle.

On a side note: If your question is answered please remember to mark this solved. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.