nodes: 0, module; 1, function_definition; 2, function_name:sort; 3, parameters; 4, block; 5, identifier:polylines; 6, expression_statement; 7, for_statement; 8, comment:"""
      sort points within polyline
      p0-p1-p2...
      """; 9, pattern_list; 10, call; 11, block; 12, identifier:n; 13, identifier:c; 14, identifier:enumerate; 15, argument_list; 16, expression_statement; 17, if_statement; 18, identifier:polylines; 19, assignment; 20, comparison_operator:l > 2; 21, comment:# DEFINE FIRST AND LAST INDEX A THOSE TWO POINTS THAT; 22, comment:# HAVE THE BIGGEST DIFFERENCE FROM A MIDDLE:; 23, block; 24, identifier:l; 25, call; 26, identifier:l; 27, integer:2; 28, expression_statement; 29, expression_statement; 30, expression_statement; 31, expression_statement; 32, expression_statement; 33, expression_statement; 34, expression_statement; 35, expression_statement; 36, expression_statement; 37, expression_statement; 38, expression_statement; 39, identifier:len; 40, argument_list; 41, assignment; 42, assignment; 43, assignment; 44, assignment; 45, assignment; 46, assignment; 47, assignment; 48, assignment; 49, assignment; 50, assignment; 51, assignment; 52, identifier:c; 53, identifier:mid; 54, call; 55, identifier:distV; 56, parenthesized_expression; 57, identifier:dists; 58, call; 59, identifier:firstI; 60, call; 61, identifier:sign; 62, call; 63, identifier:dd; 64, call; 65, subscript; 66, integer:0; 67, identifier:lastI; 68, call; 69, identifier:ind; 70, call; 71, identifier:c; 72, subscript; 73, subscript; 74, identifier:c; 75, attribute; 76, argument_list; 77, binary_operator:c - mid; 78, identifier:norm; 79, argument_list; 80, attribute; 81, argument_list; 82, attribute; 83, argument_list; 84, attribute; 85, argument_list; 86, identifier:dists; 87, unary_operator; 88, attribute; 89, argument_list; 90, identifier:_sort; 91, argument_list; 92, identifier:c; 93, identifier:ind; 94, identifier:polylines; 95, identifier:n; 96, identifier:c; 97, identifier:mean; 98, keyword_argument; 99, identifier:c; 100, identifier:mid; 101, identifier:distV; 102, keyword_argument; 103, identifier:np; 104, identifier:argmax; 105, identifier:dists; 106, identifier:np; 107, identifier:sign; 108, subscript; 109, identifier:np; 110, identifier:logical_or; 111, comparison_operator:np.sign(distV[:, 0]) != sign[0]; 112, comparison_operator:np.sign(distV[:, 1]) != sign[1]; 113, identifier:dd; 114, identifier:np; 115, identifier:argmax; 116, identifier:dists; 117, identifier:c; 118, identifier:firstI; 119, identifier:lastI; 120, identifier:axis; 121, integer:0; 122, identifier:axis; 123, unary_operator; 124, identifier:distV; 125, identifier:firstI; 126, call; 127, subscript; 128, call; 129, subscript; 130, integer:1; 131, attribute; 132, argument_list; 133, identifier:sign; 134, integer:0; 135, attribute; 136, argument_list; 137, identifier:sign; 138, integer:1; 139, identifier:np; 140, identifier:sign; 141, subscript; 142, identifier:np; 143, identifier:sign; 144, subscript; 145, identifier:distV; 146, slice; 147, integer:0; 148, identifier:distV; 149, slice; 150, integer:1
code: def sort(polylines):
      """
      sort points within polyline
      p0-p1-p2...
      """
      for n, c in enumerate(polylines):
          l = len(c)
          if l > 2:
              # DEFINE FIRST AND LAST INDEX A THOSE TWO POINTS THAT
              # HAVE THE BIGGEST DIFFERENCE FROM A MIDDLE:
              mid = c.mean(axis=0)
  
              distV = (c - mid)
              dists = norm(distV, axis=-1)
  
              firstI = np.argmax(dists)
              sign = np.sign(distV[firstI])
  
              dd = np.logical_or(np.sign(distV[:, 0]) != sign[0],
                                 np.sign(distV[:, 1]) != sign[1])
  
              dists[~dd] = 0
              lastI = np.argmax(dists)
  
              ind = _sort(c, firstI, lastI)
              c = c[ind]
              polylines[n] = c