按第二个元素对元组列表进行排序 [英] Sort a list of tuples by their second elements

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

问题描述

我想按第二个元素对元组列表进行排序.

I want to sort a list of tuples by their second elements.

示例输入:

[("Bob",3),("Terry",1)]

示例输出:

[("Terry",1)("Bob",3)]

推荐答案

另一个很酷的技巧是使用 Data.Function 中的 on:

Another cool trick is to use on from Data.Function:

import Data.Function (on)
import Data.List (sortBy)

sortBy (compare `on` snd) [...]

comparing 没有太大区别,但时不时地是个不错的技巧.

Not much different than comparing but a nice trick from time to time.

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

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