from typing import List
from typing import Tuple
from typing import Dict
from typing import Union


def sort_answers(answers: List[Tuple]) -> List[Dict[str, Union[str, float]]]:
    """ Sorts the answers of all context based on null score difference (scored_answer only) """
    sorted_answers = sorted(answers, key=lambda x: x[2], reverse=False)
    app_answers = []
    for ans in sorted_answers:
        if ans[0].lstrip().split(" ")[0] != "[CLS]": # ignore these non-answers
            mydict = {}
            mydict['text'] = ans[0]
            mydict['probability'] = ans[1] # if getting probability
            mydict['null_score_diff'] = ans[2]
            mydict['status'] = ans[3]
            mydict['context'] = ans[4]
            app_answers.append(mydict)

    return app_answers