在Ruby中按数组中的对象中的值排序 [英] Sorting by a value in an object in an array in Ruby

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

问题描述

我在数组中有一堆对象,并想按每个对象具有的值进行排序.每个对象中有问题的属性是一个数值.

I have a bunch of objects in an array and would like to sort by a value that each object has. The attribute in question in each object is a numeric value.

例如:

[[1, ..bunch of other stuff],[5, ""],[12, ""],[3, ""],]

将成为:

[[1, ..bunch of other stuff],[3, ""],[5, ""],[12, ""],]

我想按存储在每个对象中的数值排序.

I want to sort by the numerical value stored in each of the objects.

[5, 3, 4, 1, 2]变为[1, 2, 3, 4, 5],但是这些数字存储在对象内部.

[5, 3, 4, 1, 2] becomes [1, 2, 3, 4, 5], however these numbers are stored inside objects.

推荐答案

其他答案是好的,但不是最小的.怎么样?

The other answers are good but not minimal. How about this?

lst.sort_by &:first

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

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