def sort(self):
if self.sorted:<EOL><INDENT>return<EOL><DEDENT>def key(variant):<EOL><INDENT>requested_key = []<EOL>names = set()<EOL>for i, request in enumerate(self.solver.request_list):<EOL><INDENT>if not request.conflict:<EOL><INDENT>req = variant.requires_list.get(request.name)<EOL>if req is not None:<EOL><INDENT>requested_key.append((-i, req.range))<EOL>names.add(req.name)<EOL><DEDENT><DEDENT><DEDENT>additional_key = []<EOL>for request in variant.requires_list:<EOL><INDENT>if not request.conflict and request.name not in names:<EOL><INDENT>additional_key.append((request.range, request.name))<EOL><DEDENT><DEDENT>if (VariantSelectMode[config.variant_select_mode] ==<EOL>VariantSelectMode.version_priority):<EOL><INDENT>k = (requested_key,<EOL>-len(additional_key),<EOL>additional_key,<EOL>variant.index)<EOL><DEDENT>else:  <EOL><INDENT>k = (len(requested_key),<EOL>requested_key,<EOL>-len(additional_key),<EOL>additional_key,<EOL>variant.index)<EOL><DEDENT>return k<EOL><DEDENT>self.variants.sort(key=key, reverse=True)<EOL>self.sorted = True<EOL>
Sort variants from most correct to consume, to least.

        Sort rules:

        version_priority:
        - sort by highest versions of packages shared with request;
        - THEN least number of additional packages added to solve;
        - THEN highest versions of additional packages;
        - THEN alphabetical on name of additional packages;
        - THEN variant index.

        intersection_priority:
        - sort by highest number of packages shared with request;
        - THEN sort according to version_priority

        Note:
            In theory 'variant.index' should never factor into the sort unless
            two variants are identical (which shouldn't happen) - this is just
            here as a safety measure so that sorting is guaranteed repeatable
            regardless.