How to sort an array based on the length of each element?

前端 未结 9 1368
傲寒
傲寒 2020-11-28 22:31

I have an array like this:

arr = []
arr[0] = \"ab\"
arr[1] = \"abcdefgh\"
arr[2] = \"abcd\"

After sorting, the output array should be:

9条回答
  •  眼角桃花
    2020-11-28 23:23

    #created a sorting function to sort by length of elements of list
    def sort_len(a):
        num = len(a)
        d = {}
        i = 0
        while i

提交回复
热议问题