Grouping items into list / tree branches / paths according to an "id"

Hello,
I’m checking how to group items into lists or branches of tree according to an id.
Example, I want to color each face of a mesh according to a reference id, for instance representative of thickness, or material or other; so I need to construct tree paths according to this identifier.
I tried with “sift”, it works, but it’s limited because it assumes that I knew the max number of colours/thickness/tree branches I need (i.e. list length of ids):

I’ve tried also with “replace path”, but it is terribily slow, because it is checking each element face…

Note that the case is different from coloring with mesh spray, that acts on points.

Thanks in advance to any possible help.

Components is not my game … by if you want a C# that does that “by the book” (using a suitable Class - or many and then any flat/nested query imaginable for classic data mining) notify.

Thank you for the answer.
I’m a complete newbie in c#, but I’d appreciate if you show me something to solve the problem with a script, there’s alway a lot to learn…

Good news > in a few years you’ll become master of your own luck.

Back to “grouping”.

C# is an OOP thingy meaning that in the vast majority of cases you use objects defined via a Class. So if we define:

public List<INFO> info;
  public List<object> metaData;

  public class INFO {
    public int IDX {get;set;}
    public string TYPE {get;set;}
    public object OBJ {get;set;}

    //----------------------------- either this:
    public List<object> PROP {get;set;}

    //----------------------------- or explicit:
    public int PROP_1 {get;set;}
    public double PROP_2 {get;set;}

    public INFO(int idx, string type, object obj){
      this.IDX = idx; this.TYPE = type; this.OBJ = obj;
    }

    public INFO(int idx, string type, object obj, List<object> properties){
      this.IDX = idx; this.TYPE = type; this.OBJ = obj; this.PROP = properties;
    }

    public INFO(int idx, string type, object obj, int prop1, double prop2){
      this.IDX = idx; this.TYPE = type; this.OBJ = obj; this.PROP_1 = prop1; this.PROP_2 = prop2;
    }
  }

and assuming that for each Face there’s some type of metaData available (if not no big deal) we can populate the class, say like this:

 info = new List<INFO>();

    var MF = M.Faces;

    if(MF.Count != metaData.Count){Print("data mismatch"); return;}
    for(int i = 0; i < MF.Count;i++){
      object face = MF[i];
      int p1 = (int) metaData[i];
      double p2 = 0;
      // blah, blah...

      INFO inf = new INFO(i, "MeshFace", face, p1, p2);
      info.Add(inf);
    }

For a full example provide some hints about the Property (what you call id).

Is this unique? or is from some List of values? Meaning that you want to “group” (i.e. Cluster) your objects according the id values assigned? (and then doing any freaky thing/query/whatever imaginable).

It’s quite trivial with a Python script (if it’s acceptable for you):

In this example, numbers comprised between 0 and 1 are sorted according to their first decimal digit.

The ids must be hashable, i.e. a value like a string a number etc. that is immutable.

id_sorting.gh (8.7 KB)

thank you, I’ll study your code

Wow! perfect and compact script! thank you very much

1 Like

Feel free to ask if there is something you don’t understand in the script.