Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a List(Of Tuple)

I have a list like this:

Dim Results_List As New List(Of Tuple(Of String, String, Long))

With those contents:

(B, a, 5000)
(G, a, 1000)
(B, b, 8000)
(G, b, 2000)

Can I use LINQ (or whateverelse but not using a slow FOR) to sort the items by the Long numbers in ascending mode as this?:

(G, a, 1000)
(G, b, 2000)
(B, a, 5000)
(B, b, 8000)
like image 875
ElektroStudios Avatar asked Mar 27 '26 16:03

ElektroStudios


2 Answers

var sortedItems = list.OrderBy(t => t.Item3);
like image 98
SLaks Avatar answered Mar 30 '26 10:03

SLaks


Yes, even by using the internal Sort of List:

Results_List.Sort(Function(p As Tuple(Of String, String, Long), q As Tuple(Of String, String, Long)) p.Item3.CompareTo(q.Item3))

Note that this will sort the Results_List directly, while the OrderBy will returned a new, sorted, enumerable.

like image 22
xanatos Avatar answered Mar 30 '26 11:03

xanatos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!