---
title: "How to sort tuple list in Erlang?"  
description: "How to sort tuple list in Erlang?"  
author: "Anonymous User"  
published: 2016-03-11  
updated: 2016-03-14  
canonical: https://www.mindstick.com/forum/34052/how-to-sort-tuple-list-in-erlang  
category: "erlang"  
tags: ["erlang"]  
reading_time: 1 minute  

---

# How to sort tuple list in Erlang?

I am [beginner](https://www.mindstick.com/forum/23159/need-beginner-project-ideas) in Erlang programming. I want to sort according to the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) element of each [tuple](https://www.mindstick.com/forum/33652/how-to-use-tuple-in-c-sharp). I [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) use usort/1 and it [works fine](https://answers.mindstick.com/qa/115732/my-software-works-fine-on-windows-10-but-crashes-on-windows-11-why) but it only works with the first element.\
So I think we need to [swap](https://www.mindstick.com/forum/34027/how-to-swap-the-values-of-two-columns-in-sql-server) each [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) and after we do sort [operation](https://www.mindstick.com/forum/33917/how-to-callback-operation-using-delegate-in-c-sharp) and after swap it back.Can you tell me any other easiest solution to sort tuple lists.Thankyou.

## Replies

### Reply by Tarun Kumar

If you want to sort tuples according to the second element of each tuples in a list then use **keysort()** function.keysort() function is an inbuilt function in Erlang Shell which takes two parameters, in first parameter we pass the position of the element from which we want to sort and in the second parameter we pass the List. *Examples are below:*

> ```
> 1> List = [{a,b}, {c,a}, {b,c}].  // Customized List[{a,b}, {c,a}, {b,c}]2> L1 = lists:keysort(1, List).   // Sorting will be according to the first element of the tuples.[{a,b},{b,c},{c,a}]3> L2 = lists:keysort(2, List).   // Sorting will be according to the second element of the tuples.[{c,a},{a,b},{b,c}]
> ```


---

Original Source: https://www.mindstick.com/forum/34052/how-to-sort-tuple-list-in-erlang

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
