How to sorting by alphabet then by number in Dynamo?

Hi everyone,
I’m trying to sort my list by alphabet and number.
I would be grateful if someone can help me to fix this issue.

The correct order is shown below.

Hi and welcome!

I think you could use the Sort Natural node for this

What are you after using this sorted list for? There may be a better way to make sure data joins up.

Something to think of, the marks may be organised but you may need to keep the associated element sorted with it.

Maybe something like this?

I think that is one solution for you :

# Load the Python Standard and DesignScript Libraries
import re
A = IN[0]
# regex sort string by last number
OUT = sorted(A, key=lambda x: int(re.findall(r'\d+', x)[-1]))

A = ["element1","Alement 5", "Clement3", "Blement 4", "element 2"]
# regex sort string by alphabet last number increase
A = sorted(A, key=lambda x: (re.findall(r'\D+', x)[-1], int(re.findall(r'\d+', x)[-1])))
OUT = A

2 Likes

(post deleted by author)

(post deleted by author)

Thank you
Should I install any specific package?
Because I don’t have List Sort Natural and Dictionary by key values nodes.

Hi,

The Sort natural node is from the GeniusLoci package, the dictionary node is from the Springs package

This does not use any packages but also has something to think of.

Red area correlates revit elements via type mark with data from say Excel.
Orange Area just sorts the revit elements by there type marks.

Red python Code:

rvtInputList = IN[0]

excelData = IN[1]
excelMarkList = zip(*excelData)[0]

OUT=[]

for ri in rvtInputList:
	
	# Gets the list index of the item that matches
    excelDataindex = excelMarkList.index(ri[1])
    
    # Outputs the rvtInputList items with the relevant data
    # from excel Data
    OUT.append([ri[0],ri[1],excelData[excelDataindex][1]])

Orange Python Code:

inputList = zip(*IN[0])

padString = []

#Creates a new list of padded Values
for a in inputList[1]:
	padString.append("P" + a.replace("P","").zfill(3))
	
# Adds new padded string to list	
inputList.append(padString)

#Sorts list out by last sublist item
# zip with a * transposes the list
sortedList = sorted(zip(*inputList), key=lambda x: x[2])

OUT = zip(*zip(*sortedList)[0:2])
1 Like

Strings will sort character by character whereas numbers will use natural sorting (value). Just convert the numeric part of the key to a number so it sorts naturally.

1 Like