packages=['regex',]

Groovy sorting

Sort functions by name, alphabetically ascending.

Regex: '/[a-zA-Z]+\s[A-Za-z0-9]+\([^)]*\)\s+\{/g'
Example: void myFunc(String instring) {...}
    from pyodide.ffi import create_proxy import regex from js import document, console def sortkey(): """Return the basename of function.""" return lambda n: n.split("(")[0].split(" ")[-1] def hello_args(self): finalreg = r"[a-zA-Z]+\s[A-Za-z0-9]+\([^)]*\)\s*\{((?:[^{}]|\{(?1)*\})*)\}" text = document.getElementById("infile").innerHTML matches = regex.finditer(finalreg, text) # Get all matches as strings in list out = [] for match in matches: outstring = "".join(match.group()) out.append(outstring) # Sort by function name before first bracket outsort = sorted(out, key=sortkey()) # Remove all found functions from text and strip trailing whitespace for i in outsort: text = text.replace(i, "") text = text.rstrip('<br>') text = text.strip() # Now add them in sorted order text += '\n' for i in outsort: text += f'\n{ i }\n' document.getElementById("mycode").value = text Element('button').element.addEventListener("click", create_proxy(hello_args))