简体   繁体   中英

Sorting Array of Arrays having variable number of elements

I have to sort an array of arrays. I've searched for solutions however my problem is:

  1. need to sort arrays that may have different sizes from a script run to another.
  2. need to sort not only by one or two elements, but, if possible based in all elements.

For example, for the following inputs:

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

The output should be, respectively:

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

Do as below

input=[[2,3,4,5,6],[1,3,4,5,7],[1,3,4,5,8]] 
input.sort # => [[1, 3, 4, 5, 7], [1, 3, 4, 5, 8], [2, 3, 4, 5, 6]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM