首先,需定义一个包含内部列表的实体类(Entity),然后根据内部列表中字符串长度来实现排序。
代码示例:
class Entity: def init(self, internal_list): self.internal_list = internal_list
def __str__(self):
return str(self.internal_list)
def __lt__(self, other):
return len(self.internal_list) < len(other.internal_list)
example_list = [Entity(['a', 'bc']), Entity(['d', 'efg', 'hi']), Entity(['jkl'])]
sorted_list = sorted(example_list)
for entity in sorted_list: print(entity) #输出:['jkl'], ['a', 'bc'], ['d', 'efg', 'hi']
上一篇:按内部类属性值分组
下一篇:按内部属性过滤的JSON数组