按两个值对数组进行排序 [英] Sorting an array by two values

查看:59
本文介绍了按两个值对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

an_array = [[2, 3], [1, 4], [1, 3], [2, 1], [1, 2]]

我想先按每个内部数组的第一个值对这个数组进行排序,然后按第二个值排序(因此排序后的数组应如下所示:[[1, 2], [1, 3], [1, 4], [2, 1], [2, 3]])

I want to sort this array by the first value of each inner array, and then by the second (so the sorted array should look like this: [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3]])

最易读的方法是什么?

推荐答案

这是排序数组的默认行为(请参阅 Array#<=> 证明方法定义).你应该能够做到:

This is the default behavior for sorting arrays (see the Array#<=> method definition for proof). You should just be able to do:

 an_array.sort

这篇关于按两个值对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆