Sorting of 2D Array by its amount of in the inner elements

前端 未结 2 824
星月不相逢
星月不相逢 2021-01-25 01:42

How do I sort a 2D array by the length of its inner elements? The number of inner elements is not the same.

Example:

a = [[1], [2, 3], [4, 5, 6, 7], [8,          


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 02:35

    Try this:

    a = [[1], [2, 3], [4, 5, 6, 7], [8, 9]]
    a.sort { |x, y| y.size <=> x.size }
    #=> [[4, 5, 6, 7], [2, 3], [8, 9], [1]]
    

提交回复
热议问题