简体   繁体   English

如何在多维数组中按字母顺序排序?

[英]How to sort alphabetically within a multidimensional array?

I'm an intermediate Ruby developer. 我是Ruby中级开发人员。 I'm working on some puzzles to brush up on Ruby and I'm trying to figure out how to go about sorting a multidimensional array. 我正在研究一些难题,以复习Ruby,并且试图弄清楚如何对多维数组进行排序。 Here's my code: 这是我的代码:

myArray = [['Jones', 'Layla'], ['Smith', 'Gary'], ['Williams', 'Nick'] ,['Brown', 'Kyle']]

So I need to iterate through the array and through the sub-arrays, identify the last names, and alphabetize them. 因此,我需要遍历数组和子数组,确定姓氏并将它们按字母顺序排列。 For some reason nothing I've been trying for the last two hours seems to be working. 由于某种原因,最近两个小时我一直没有尝试的方法似乎没有起作用。 I've been searching high and low online. 我一直在网上搜索高低。 Can anyone help out? 有人可以帮忙吗? I've been trying variations of the following, below. 我一直在尝试以下变化。 Any pointers? 有指针吗?

myArray.each { |sub_array| 
sub_array.sort_by! { |x|
x[0].downcase #the x[0] element being the last name, which is the first element in each of the sub-arrays
    }
return sub_array
}

edit: forgot to clarify, the end result should look like this: 编辑:忘记澄清,最终结果应如下所示:

myArray = [['Brown', 'Kyle']['Jones', 'Layla'], ['Smith', 'Gary'], ['Williams', 'Nick']]

If you want sort the array by the lastname (the first element of each sub array) you could do easily with this: 如果要按姓(每个子数组的第一个元素)对数组进行排序,则可以轻松地做到这一点:

myArray.sort { |x,y| x[0] <=> y[0] }

myArray = [['Jones', 'Layla'], ['Smith', 'Gary'], ['Williams', 'Nick'], ['Brown', 'Kyle']]
 => [["Jones", "Layla"], ["Smith", "Gary"], ["Williams", "Nick"], ["Brown", "Kyle"]] 
myArray.sort { |x,y| x[0] <=> y[0] }
 => [["Brown", "Kyle"], ["Jones", "Layla"], ["Smith", "Gary"], ["Williams", "Nick"]]  

sort documentation here 在此处 sort文档

Just use Array#sort without a block: 只需使用Array#sort不带任何块:

my_array = [['Jones', 'Layla'], ['Smith', 'Gary'], ['Williams', 'Nick'],
            ['Brown', 'Kyle']]
my_array.sort
   #=> [["Brown", "Kyle"], ["Jones", "Layla"], ["Smith", "Gary"], ["Williams", "Nick"]]

See the doc for the method Array#<=> (the spaceship operator ) to learn how Ruby orders each pair of arrays within the sorting algorithm. 有关方法Array#<=>太空飞船操作员 ),请参阅文档,以了解Ruby如何在排序算法中对每对数组进行排序。

If two people have the same last name, sort will order them by their first name (ie, first names are used to break ties): 如果两个人的姓氏相同,则sort将按其姓氏sort他们进行sort (即,名字用于打破关系):

my_array = [['Jones', 'Layla'], ['Smith', 'Gary'], ['Williams', 'Nick'],
            ['Brown', 'Kyle'], ['Jones', 'Zack']]

my_array.sort
  #=> [["Brown", "Kyle"], ["Jones", "Layla"], ["Jones", "Zack"],
  #    ["Smith", "Gary"], ["Williams", "Nick"]] 

I used my_array instead of myArray because the Ruby convention is to use "snake-case" for names of variables and methods. 我使用my_array代替myArray因为Ruby约定是对变量和方法的名称使用“小写字母”。 You don't have to follow that convention, but 99%+ of Rubiests do. 您不必遵循该惯例,但是99%以上的Rubiesst可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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