如何对字典中的元素进行排序

问题:

希望对字典中的key/value 进行排序 

思路:

1. 将字典转换为数组。

2. 对该数组进行排序

3. 将排过序的数组的key/value提出来

代码:

    def sort_by_time(self,files,remote_dir):
        files_hash={}
        self.ftp.cwd(remote_dir)
        filesbysort=[]
        for f in files:
            files_hash[self.ftp.sendcmd('MDTM '+f)[3:].strip()]=f
        
        items=files_hash.items()
        items.sort()
        for key,value in items:
            filesbysort.append(value)
            

        return filesbysort 

原文地址:https://www.cnblogs.com/morebetter/p/2644297.html