Reorder a list based on strings in common in sublists

Hello guys!

Its my first time posting here, so please let me know if there is something wrong or im missing something with my post.

So my problem is this: I have a list of list of areas, where each list contains four elements, the name of the type of area, the floor the area is situated, the size of the area and finally, the name of the renter of the area. I need to be able to sort the list based on the latter. So that all the areas rented by A is in one list, all areas rented by B is in another, and so on.

I have tried using list.GroupByKey, but with this I am only able to group the renter element, without including the other 3 elements. The point is to calculate the total area of each renter

Thanks!

Try not using the list @ level for the input of your list that you’re trying to group.

Hope this helps!

Hi @oyvind.s

here is a solution.

key[0] indicates that sorting is performed based on the first element. Here you can change it as you want.

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

input_list = IN[0]

import re 

def natural_sort(l): 
	convert = lambda text: int(text) if text.isdigit() else text.lower() 
	alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key[0])] 
	return sorted(l, key = alphanum_key)

OUT = natural_sort(input_list)

Thank you! This helped a lot :slight_smile:

1 Like