Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting tuples in F#

Tags:

.net

sorting

f#

I have a list of tuples representing coordinates of points. How can I sort them by the first or second value, so that I could order my points from left to right first and from top to bottom next?

like image 844
Piotr Zurek Avatar asked Apr 30 '09 10:04

Piotr Zurek


People also ask

How do you sort a list of tuples?

In python, to sort list of tuples by the first element in descending order, we have to use the sort() method with the parameter ” (reverse=True) “ which will sort the elements in descending order.

How do you sort a tuple by key?

Given tuples, we need to sort them according to any given key. This can be done using sorted() function where we sort them using key=last and store last as the key index according to which we have to sort the given tuples. # according to given key.

How do you sort a tuple in Python without sorting?

You can use one of the easiest sorting algorithm that is bubble sort. In case of tuples in tuple it will sort in order of first element of tuple. So ((32, "b"), (1, "c"), (23,"a")) Now if you sort it it will sort it in the order of 1st element of tuple.

How do you sort a tuple alphabetically in Python?

In Python, there are two ways, sort() and sorted() , to sort lists ( list ) in ascending or descending order. If you want to sort strings ( str ) or tuples ( tuple ), use sorted() .


1 Answers

Sounds like you want e.g.

myList |> List.sortBy fst

http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/FSharp.Core/Microsoft.FSharp.Collections.List.html

But tuples support structural equality and comparison, so the default sort (lexicographical) may do what you want.

like image 149
Brian Avatar answered Oct 18 '22 19:10

Brian