title: sorting values between open and closed parenthesis iteratively in python
body: <p>I am trying to sort values alphabetically between open and closed parenthesis like the following </p>
  
  <p><strong>(m,b,l,a(d,g,c(l,e)),f(k,g,h(i)),j)</strong> and I would like a output of</p>
  
  <p><strong>(a(c(e,l),d,g),b,f(g,h(i),k),j,l,m)</strong> in the sorted order.</p>
  
  <p>I have done some coding and it is going wacky. I was able to sort innermost parenthesis and but not able continue.</p>
  
  <pre><code>indexofparen = []
  for i, c in enumerate(s):
      if c == "(":
          indexofparen.append(i)
  ##print(indexofparen)
  closeparen = []
  for i, c in enumerate(s):
      if c == ")":
          closeparen.append(i)
  ##print(closeparen)
  parenindex=s.rindex("(")
  ##print(parenindex)
  matchparen=s.index(")")-1
  ##print(matchparen)
  list_val = s[s.rindex("(")+1:s.index(")")].split(",")
  ##print("Before:", list_val)
  for passnum in range(len(list_val)-1, 0, -1):
      for i in range(passnum):
          if list_val[i] &gt; list_val[i+1]:
            list_val[i], list_val[i+1] = list_val[i+1], list_val[i]
  ##print(list_val)
  s1=s[:parenindex] + "(" + ','.join(str(e) for e in list_val) + ")" + s[matchparen:]
  ##print(s1)
  s2 = s[indexofparen[1]+1:closeparen[2]]
  </code></pre>
  
  <p>After this I am kind of loopy. Any help is to refine and go about sorting this hard problem is appreciated. Thank you very much for the time spent in helping me. Much appreciated.</p>