Verstion of List.MatchWithKeyValues in DesignScript

I’ve got two corresponding lists. One list of lines/curves and another of strings. The lines list eventually gets reordered in my graph and I need to get the strings list in the same order.

With something like text as my original and key list I can use List.MatchWithKeyValues to generate a set of values (I am using a range matching the index of the original list) that I can use in a List.GetItemAtIndex on the strings list to reorder them. The problem is the MatchWithKeyValues doesn’t work with lines.

DesignScript has an IsAlmostEqualTo function (?) that works but I don’t see a way I understand to do this in Python so… I’m wondering if we can translate the MatchWithKeyValues to DesignScript.

This just returns null;

def KeyValue(items:var[]..[],keys:var[]..[],values:var[]..[])
{
return = [Imperative]
{
lst:var[];
for (i in items)
	c = 0;
	for (k in keys)
		{
		if (i.IsAlmostEqualTo(k) && true)
			lst = values[c];
			return=lst;
                c = c + 1;
		}
}
};

You might be able to tell… I don’t really know what I’m doing yet. I just sort of hack away until I get something that works!

Of course, if there’s another solution I’m all ears! Thanks.

It depends on how you sort the original list :slight_smile:

Let’s start from naive way of getting all indices in original list and reorder string list. Firstly you could write a helper function like this:

def getIndex(list:Geometry[], element:Geometry)
{
    return = [Imperative]
    {
        count = List.Count(list);
        for (i in 0..count)
        {
           if (element.IsAlmostEqualTo(list[i]))
           {
               return = i;
           }
        }
        return = -1;
    }
}

And then use this function to get indices of geometries in original list, and get reordered strings:

The other way is to associate the geometry list with string list. That is, you can create a list whose each item is a list which contains two items: {geometry, string}. For example:

Now we can sort this list based on the first item of each sublist. Say, we try to sort based on the Y coordinates by using List.SortByKey:

Through this way we can associative two lists together and dont need to reorder string list.