Optimized way to reorder numbering using C #

I have the following items displayed on an asp.net (C #) page.

1 Welcome
2 What's New? 2.1 Gifts
2.2 Ideas
2.3 Other
2.3.1 New
2.3.2 Boats
2.4 Vehicals
2.5 Fruits

Now the user can delete any child elements (not root so that the user can remove the gifts of clause 2.1 or the novelty of clause 2.3.1). After the user removes the item, I need to re-sketch the structure using C #. I am looking for any suggestions / ideas / code to complete this task.

1 Welcome
2 What's New? 2.1 Gifts (delete)
2.2 Ideas
2.3 Others
2.3.1 New (remove)
2.3.2 Boats
2.4 Vehicals
2.5 Fruits

The result should be -

1 Welcome
2 What's New? 2.1 Ideas
2.2 Others
2.2.1 New (Remove)
2.2.2 Boats
2.3 Vehicals
2.4 Fruits

+3
source share
5 answers

, , . , , . , , , .

List<Int32> numbering = new List<Int32>();

Int32 lastNestingLevel = -1;

foreach (String line in lines)
{
    String[] parts = line.Split(new Char[] { ' ' }, 2);

    Int32 currentNestingLevel = parts[0].Count(c => c == '.');

    if (currentNestingLevel > lastNestingLevel)
    {
        // Start a new nesting level with number one.
        numbering.Add(1);
    }
    else if (currentNestingLevel == lastNestingLevel)
    {
         // Increment the number of the current nesting level.
        numbering[currentNestingLevel] += 1;        }
    else if (currentNestingLevel < lastNestingLevel)
    {
         // Remove the deepest nesting level...
        numbering.RemoveAt(numbering.Count - 1);
         // ...and increment the numbering of the current nesting level.
        numbering[currentNestingLevel] += 1;
    }

    lastNestingLevel = currentNestingLevel;

    String newNumbering = String.Join(".", numbering
        .Select(n => n.ToString())
        .ToArray());

    Console.WriteLine(newNumbering + " " + parts[1]);
}

List<String> lines = new List<String>()
{
    "1 Welcome",
    "2 Whats New",
    //"2.1 Gifts",
    "2.2 Ideas",
    "2.3 Others",
    //"2.3.1 Novelty",
    "2.3.2 Boats",
    "2.4 Vehicals",
    "2.5 Fruits"
};

.

1 Welcome
2 Whats New
2.1 Ideas
2.2 Others
2.2.1 Boats
2.3 Vehicals
2.4 Fruits

UPDATE

- , .

Dictionary<Int32, Int32> numbering = new Dictionary<Int32, Int32>();

Int32 lastNestingLevel = -1;

foreach (String line in lines)
{
    String[] parts = line.Split(new Char[] { ' ' }, 2);

    Int32 currentNestingLevel = parts[0].Count(c => c == '.');

    if (currentNestingLevel > lastNestingLevel)
    {
        numbering[currentNestingLevel] = 1;
    }
    else
    {
        numbering[currentNestingLevel] += 1;
    }

    lastNestingLevel = currentNestingLevel;

    String newNumbering = String.Join(".", numbering
        .Where(n => n.Key <= currentNestingLevel)
        .OrderBy(n => n.Key)
        .Select(n => n.Value.ToString())
        .ToArray());

    Console.WriteLine(newNumbering + " " + parts[1]);
}

. , .

2.3.2.1 Vehicals
2.5 Fruits <= nesting level drops by two

, , , , , , .

+2

"" ? , , , .

+5

: XML, XSL ...

+2

. , "2" (2.1 - ), "" , 2.1 2.2

"1" , 1.1
, ...

, :
"L1", .
:

List L1[2]
  List Welcome[0]
  List What New[5]
    List Gifts[0]
    List Ideas[0]
    List Others[2]
      List Novelty[0]
      List Boats[0]
    List Vehicles[0]
    List Fruits[0]

, , .

"Item", String ItemName, . , .

+1

:

public class Content
{
    public string Title { get; set; }
    public IList<Content> SubContent
    {
        get;
        private set;
    }

    private Content()
    {
    }

    public Content(string title)
    {
        Title = title;
        SubContent = new List<Content>();
    }
}

/ :

    private static void Display(string marker, IList<Content> content)
    {
        int count = 0;
        foreach (Content c in content)
        {
            string label = marker + (marker.Length > 0 ? "." : "") + (++count);
            Console.WriteLine(label + " " + c.Title);
            if (c.SubContent.Count > 0)
                Display(label, c.SubContent);
        }
    }
+1

Source: https://habr.com/ru/post/1711682/


All Articles