Problems with layer list order

To get the same order as layers are kept in the Rhino layer dialog, I am using the “SortIndex” from each layer in the Rhino layer table. This is working perfect as long as the “Sortindex” from each layer in the Rhino layer table is correct.

My problem is that the sortindexes seems to be out of order compared to the Rhino layer dialog in some cases.

I have noticed that it is possible to refresh the Rhino layer list and get correct sortindexes if manually moving one layer up and down in the Rhino layer dialog.

I am trying to find a solution to refesh the Rhino layer list from Rhinocommon, but I have not been able to find anything that works so far. Below is what I have tried, but nothing works for me.

                   // Work around to update the layer list.
                    Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;
                    int napplayer = doc.Layers.Find(Mc.appstr, true);
                    if (napplayer >= 0)
                    {
                        doc.Views.RedrawEnabled = false;
                        Rhino.DocObjects.Layer fakelayer = new Rhino.DocObjects.Layer();
                        Rhino.DocObjects.Layer app_layer = doc.Layers[napplayer];
                        fakelayer.ParentLayerId = app_layer.Id;
                        int nlayer = doc.Layers.Add(fakelayer);
                        doc.Layers.Delete(nlayer, true);
                        doc.Views.RedrawEnabled = true;
                    }

Thanks,

Joakim