Why Insertion Sort Of Property
Sorting in Dynamo involves many unnecessary visual nodes which complicates the script. Insertion sort of property is similar in output with Double Sort node but instead of pre-extracting the values to be sorted you just pass as a string the name of the proeperty
Description
Let’s say you have a set of points and you want to order them by their X values.
Use insertion node of property by passing the list of points and in a text box the value “X”.
In this image you can see a set of points that are ordered by their X property values. There are 2 returns, one list representing the sorted X values and the second list representing the re-ordered points according to their corresponding X.
Solution
The code of achieving this involves using Reflection to determine if the property of field exists and using insertion sort to rearrange properties.
This solution will also sort elements along with their properties.
<pre class="theme:vs2012-black lang:c# decode:true "> public static object InsertionSortOfItemProperty(object data, string propertyName) { /// if no list is provided return the entry element if (!(data is IList)) return new object[2] { 0, data }; ///check list dimension IList listentry = (IList)data; foreach (var item in listentry) if (item is IList) throw new Exception("Provide a single-dimension list of elements"); ///collect data IList<double> propValues = new List<double>(); foreach (var item in listentry) { Type itemtype = item.GetType(); ///get info PropertyInfo pinfo = itemtype.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); FieldInfo fInfo = itemtype.GetField(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (pinfo == null && fInfo == null) throw new Exception("No Property/Field by this name"); ///get property value object propValue; if (pinfo != null) propValue = pinfo.GetValue(item); else propValue = fInfo.GetValue(item); ///check data type if (!(BLSystem.BLSystem.Common.Is.IsNumber(propValue))) throw new Exception(string.Format("Property is not a number : {0}", propValue)); propValues.Add(double.Parse(propValue.ToString())); } ///Sort double newValue = 0; object newValueEntry; int idy = 0; for (int idx = 0; idx < propValues.Count(); idx++) { newValue = propValues[idx]; newValueEntry = listentry[idx]; idy = idx; if (idy > 0) while (idy > 0 && propValues[idy - 1] > newValue) { propValues[idy] = propValues[idy - 1]; listentry[idy] = listentry[idy - 1]; idy--; } propValues[idy] = newValue; listentry[idy] = newValueEntry; } return new object[2] { propValues, listentry }; } </pre>