from pathlib import Path
import re


def get_files(folder, name, indexes=None):
    """Get all files with given name in given folder (and in given order)

    Parameters
    ----------
    folder : str
        path to files (folder name)
    name : str
        file name pattern
    indexes : list
        files order

    Returns
    -------
    list
        a list of file names
    """

    nums = []
    files = []
    for path in Path(folder).glob(name):
        numbers = [int(d) for d in re.findall(r'-?\d+', path.name)]
        nums.append(numbers[-1] - 1 if len(numbers) > 0 else -1)
        files.append(path.name)
    if indexes is not None:
        files = [x for _, x in sorted(zip(indexes, files))]
    else:
        files = [x for _, x in sorted(zip(nums, files))]
    return files