按内部列表的特定索引对列表列表进行排序 [英] Sorting list of lists by particular index of inner lists

查看:369
本文介绍了按内部列表的特定索引对列表列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的列表:

I have a list that looks something like this:

[["Local 7" 1 "say" "Say: Inspect Fences"] ["Local 7" 1 "do" "Do: Shepherd Cows"] ["Local 6" 1 "say" "Say: Shepherd Cows"] ["Local 6" 1 "do" "Do: Shepherd Cows"] ["Local 6" 2 "say" "Say: Shepherd Cows"] ["Local 6" 2 "do" "Do: Shepherd Cows"] ["Local 7" 2 "say" "Say: Inspect Fences"] ["Local 7" 2 "do" "Do: Shepherd Cows"] ["Local 6" 3 "say" "Say: Shepherd Cows"] ["Local 6" 3 "do" "Do: Shepherd Cows"] ["Local 7" 3 "say" "Say: Inspect Fences"] ["Local 7" 3 "do" "Do: Inspect Fences"]]

我想按item 1对列表进行排序. (我知道它已经在复制/粘贴的版本中,但是可能并不总是如此.)

I'd like to sort the list by item 1. (I know it already is in the copy/pasted version, but it might not always be.)

sort只是返回一个空列表(我什至不知道为什么,但是我想那是一个单独的问题),并且sort-by似乎不起作用,因为它需要一个解析为布尔值的报告器.

sort just returns an empty list (I'm not even sure why, but I guess that's a separate question), and sort-by doesn't seem to work because it needs a reporter that resolves to a boolean.

有一个聪明的方法可以做到这一点吗?还是我需要首先获取要排序的值的列表,然后对其进行排序,然后在该列表上进行迭代,从而在原始列表中创建一个与各个项目值匹配的值的新列表? >

Is there a clever way to do this? Or would I need to first get a list of the values I want to sort by, then sort that, and then iterate over that list, creating a new list of the values in the original list in which the respective item value matches?

推荐答案

使用NetLogo 6语法更新

Updated with NetLogo 6 syntax

您可以轻松地将其转换为sort-by:

You can convert this into a sort-by pretty easily:

to-report sort-with [ key lst ]
  report sort-by [ [a b] -> (runresult key a) < (runresult key b) ] lst
end

然后您像这样使用它:

sort-with [ l -> item 1 l ] my-list

这篇关于按内部列表的特定索引对列表列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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