def entity_by_md5(syn, contentMd5, parentId=None, cmp=None):
    """Gets the first entity in a list of entities identified by md5.

    Optionally takes a comparison function to pass to sorted
    and a parent id for filtering.

    """
    res = syn.restGET('/entity/md5/%s' % (contentMd5,))['results']
    if cmp:
        res = sorted(res, cmp=cmp)
    if parentId:
        res = filter(lambda x: x['parentId'] == parentId, res)
    try:
        entity = syn.get(res[0]['id'], version=res[0]['versionNumber'])
    except KeyError:
        entity = None
    return entity
