Sort function in Python

I created a Dynamo to collect the mark values from assembly members. when i sort inverse the list, 10 and 11 coming after 2.

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import sys
import math
clr.AddReference('RevitServices')
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
clr.AddReference('RevitNodes')
from  Autodesk.Revit.UI import *
from  Autodesk.Revit.DB import *
import Revit
clr.ImportExtensions(Revit.Elements)
from RevitServices import *
import Autodesk.Revit.UI.Selection
from Autodesk.Revit.UI.Selection import ISelectionFilter
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
clr.ImportExtensions(Revit.Elements)

# The inputs to this node will be stored as a list in the IN variables.


#selIds = uidoc.Selection.GetElementIds()

class AssemblyFilter(Selection.ISelectionFilter):
	def __init__(self):
		pass
	def AllowElement(self, ref):
		if ref.Category.Name == "Assemblies" :
			return True
		else:
			return False
	def AllowReference(self, element):
		return true
		
select = uidoc.Selection
obt1 = Selection.ObjectType.Element
selectassembly = select.PickObject(obt1.Element, AssemblyFilter())
ducts = []

elt1 = doc.GetElement(selectassembly)
elt = doc.GetElement(selectassembly.ElementId)
memberids = elt.GetMemberIds()

members = []
marklist = []
for i in memberids:
	try:
		marklist.append(doc.GetElement(i).LookupParameter("Mark").AsString())
		members.append(doc.GetElement(i))
	except:
		pass	
markset = set(marklist)	
sortedmarklist = []
sortedmarklst = []
for ii in markset:
	sortedmarklist.append(ii)
	#sortedmarklst.append(ii)
sortedmarklist.sort(reverse = True)
#sortedmarklst.sort()
breakindex = sortedmarklist.index(str(IN[1]))

"""
count = breakindex
sortedmarklistlen = len(sortedmarklist)
limit = sortedmarklistlen - 1

while count < limit :
	sortedmarklist.pop(breakindex + 1)
	count += 1
	
if IN[2] < 0 :
	sortedmarklist.sort()
	
finalmarkList = []
"""
"""
for mem in members:
	for li in sortedmarklist:
		if mem.LookupParameter("Mark").AsString() == str(li):
			TransactionManager.Instance.EnsureInTransaction(doc)
			fm = mem.LookupParameter("Mark").Set(str(int(li) + IN[2]))
			TransactionManager.Instance.TransactionTaskDone()
			finalmarkList.append(fm)
"""
OUT = sortedmarklist#,finalmarkList

It is probably sorting them as strings, try making them into integers in Python using int(value) before sorting them. If you need them as strings you can turn them back post-sort using str(value), or sort them using the sorted statement and a lambda function like this:

newList = sorted(stringList, key=lambda x: int(x))

I generally use strings for the sorted() statement but Iā€™m fairly sure it supports numbers and integers also.

The trick is that all of the marks must be valid as an integer however.

1 Like