<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _collect_mdsec_elements(files):
 """ Return all dmdSec and amdSec classes associated with the files. Returns all dmdSecs, then all amdSecs, so they only need to be serialized before being appended to the METS document. :param List files: List of :class:`FSEntry` to collect MDSecs for. :returns: List of AMDSecs and SubSections """

        dmdsecs = []
        amdsecs = []
        for f in files:
            for d in f.dmdsecs:
                dmdsecs.append(d)
            for a in f.amdsecs:
                amdsecs.append(a)

        dmdsecs.sort(key=lambda x: x.id_string)
        amdsecs.sort(key=lambda x: x.id_string)
        return dmdsecs + amdsecs