nodes: 0, module; 1, function_definition; 2, function_name:sorted_analyses; 3, parameters; 4, block; 5, identifier:self; 6, identifier:analyses; 7, expression_statement; 8, expression_statement; 9, function_definition; 10, comment:# Now, we need the analyses within a request ID to be sorted by; 11, comment:# sortkey (sortable_title index), so it will appear in the same; 12, comment:# order as they appear in Analyses list from AR view; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, for_statement; 17, comment:# Sort the last set of brains we've collected; 18, expression_statement; 19, expression_statement; 20, return_statement; 21, comment:"""Sort the analyses by AR ID ascending and subsorted by priority
          sortkey within the AR they belong to
          """; 22, assignment; 23, function_name:sorted_by_sortkey; 24, parameters; 25, block; 26, assignment; 27, assignment; 28, assignment; 29, identifier:analysis; 30, identifier:analyses; 31, block; 32, assignment; 33, call; 34, identifier:sorted_analyses; 35, identifier:analyses; 36, call; 37, identifier:objs; 38, return_statement; 39, identifier:current_sample_id; 40, None; 41, identifier:current_analyses; 42, list; 43, identifier:sorted_analyses; 44, list; 45, expression_statement; 46, if_statement; 47, comment:# Now we are inside the same AR; 48, expression_statement; 49, continue_statement; 50, identifier:current_analyses; 51, call; 52, attribute; 53, argument_list; 54, identifier:sorted; 55, argument_list; 56, call; 57, assignment; 58, boolean_operator; 59, comment:# Sort the brains we've collected until now, that; 60, comment:# belong to the same Analysis Request; 61, block; 62, call; 63, identifier:sorted_by_sortkey; 64, argument_list; 65, identifier:sorted_analyses; 66, identifier:extend; 67, identifier:current_analyses; 68, identifier:analyses; 69, keyword_argument; 70, identifier:sorted; 71, argument_list; 72, identifier:sample_id; 73, call; 74, identifier:sample_id; 75, comparison_operator:current_sample_id != sample_id; 76, expression_statement; 77, expression_statement; 78, expression_statement; 79, expression_statement; 80, attribute; 81, argument_list; 82, identifier:current_analyses; 83, identifier:key; 84, lambda; 85, identifier:objs; 86, keyword_argument; 87, attribute; 88, argument_list; 89, identifier:current_sample_id; 90, identifier:sample_id; 91, assignment; 92, call; 93, assignment; 94, assignment; 95, identifier:current_analyses; 96, identifier:append; 97, identifier:analysis; 98, lambda_parameters; 99, call; 100, identifier:key; 101, lambda; 102, identifier:analysis; 103, identifier:getRequestID; 104, identifier:current_analyses; 105, call; 106, attribute; 107, argument_list; 108, identifier:current_sample_id; 109, identifier:sample_id; 110, identifier:current_analyses; 111, list; 112, identifier:an; 113, attribute; 114, argument_list; 115, lambda_parameters; 116, call; 117, identifier:sorted_by_sortkey; 118, argument_list; 119, identifier:sorted_analyses; 120, identifier:extend; 121, identifier:current_analyses; 122, identifier:an; 123, identifier:getRequestID; 124, identifier:an; 125, attribute; 126, argument_list; 127, identifier:current_analyses; 128, identifier:an; 129, identifier:getPrioritySortkey
code: def sorted_analyses(self, analyses):
          """Sort the analyses by AR ID ascending and subsorted by priority
          sortkey within the AR they belong to
          """
          analyses = sorted(analyses, key=lambda an: an.getRequestID())
  
          def sorted_by_sortkey(objs):
              return sorted(objs, key=lambda an: an.getPrioritySortkey())
  
          # Now, we need the analyses within a request ID to be sorted by
          # sortkey (sortable_title index), so it will appear in the same
          # order as they appear in Analyses list from AR view
          current_sample_id = None
          current_analyses = []
          sorted_analyses = []
          for analysis in analyses:
              sample_id = analysis.getRequestID()
              if sample_id and current_sample_id != sample_id:
                  # Sort the brains we've collected until now, that
                  # belong to the same Analysis Request
                  current_analyses = sorted_by_sortkey(current_analyses)
                  sorted_analyses.extend(current_analyses)
                  current_sample_id = sample_id
                  current_analyses = []
  
              # Now we are inside the same AR
              current_analyses.append(analysis)
              continue
  
          # Sort the last set of brains we've collected
          current_analyses = sorted_by_sortkey(current_analyses)
          sorted_analyses.extend(current_analyses)
          return sorted_analyses