3

hey I'm trying to split a list based on if a bool of the element is true or not. but each time it has passed some true's and encounters a false i want it also to start a new list with the all the false until it encounters true again and so on. so basicly grouping sequences of falses and trues

public void SortWalls()
{
    List<Node> innerWallNodes;
    foreach(Wall w in walls)
    {
        WallAxis ax = w.axis;
        innerWallNodes = new List<Node>();
        for(int i=w.wallNodes.Count-1; i>=0; i--)
        {
            if(w.wallNodes[i].markedForDoor)
            {
                //split wall!!
                innerWallNodes.Add(w.wallNodes[i]);
                w.wallNodes.RemoveAt(i);
            }
        }
        if(innerWallNodes.Count > 0)
        {
            Wall wall = new Wall(innerWallNodes, ax);
            innerWalls.Add(wall);
        }
    }
}

i did it like this and then build a mesh based on the first and last element of a List. but since there are many scenarios where the innerWallNodes could be somewhere in the middle of the list that get "cut out" and so my remaining "outer wall" would still have the same node index in my grid for the first and last in it's list, still overdrawing my "inner wall"

so lets say every node !markedForDoor is 0 and a every node markedForDoor is 1 and they order something like below in my list. like this:

|000|11111|00000|11|000| how would i get a list for every between |...| ?

how do i do this in a simple way. I thought Linq would have something for this but can't find anything.

3
  • Can you provide a short example of the resulting lists you want to get back based on that example input? Commented Dec 19, 2015 at 20:48
  • Could you edit and put the result expect for this list? Btw, whats the type of your List? its a String? Commented Dec 19, 2015 at 20:52
  • Possible duplicate of How to group ranges in LINQ Commented Dec 19, 2015 at 20:59

3 Answers 3

2

Linq won't help. Here is the code:

List<List<YouObjectType>> SplitList(List<YourObjectType> listToSplit) {
    List<List<YouObjectType>> listOfLists = new List<List<YourObjectType>>();
    List<YourObjectType> tmp = new List<YourObjectType>();
    foreach(YourObjectType item in listToSplit) {
        if (tmp.Count > 0
            && tmp[tmp.Count - 1] != item) {
            // Compare you items here as you wish, 
            // I'm not sure what kind of objects
            // and what kind of comparison you are going to use
            listOfLists.Add(tmp);
            tmp = new List<YourObjectType>();
        }
        tmp.Add(item);
    }
    if (tmp.Count > 0) {
        listOfLists.Add(tmp);
    }
    return listOfLists;
}
Sign up to request clarification or add additional context in comments.

3 Comments

You don't think he could use a GroupBy? As he enumerates through his groups he could add his objects to a list and each time the key changes he adds to different list.
@Rottjung Put your comparison (instead of) here tmp[tmp.Count - 1] != item, Like tmp[tmp.Count - 1].markedForDoor != item.markedForDoor
Ah ok thanks Ivan now i see how it should work :-) been staring too long at the screen today...
2

Here is a simple way of doing that (no Linq)

List<Node> input = ...;
var output = new List<List<Node>>();
for (int end = 0; end < input.Count; )
{
    int start = end;
    while (++end < input.Count && input[end].markedForDoor == input[start].markedForDoor) { }
    output.Add(input.GetRange(start, end - start));
}

Comments

0

Lookup Group Results by Contiguous keys on MSDN. See how it applies to your case on Rextester.

2 Comments

trying to get my head around this :-)
i get them grouped but how do i get the groups as separate Lists? if i use a for loop i still get Lists with one item in them. part of my problem is how to iterate to get a new list/group in query?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.