Edouard Durand

Edouard Durand

  • NA
  • 46
  • 20.9k

How to sort in C# a tuple that contains several elements?

Apr 11 2022 9:44 PM

Hi,

I would like to sort in ascending order a tuple starting from its 2nd element:

    List<Tuple<string, string, string>> tupleSpecial = new List<Tuple<string, string, string>>

The 2nd element of this tuple actually represents a number, it is stored here as a string, but the sorting would be done according to this 2nd element.

I have already thought of reconstructing a tuple by converting the 2nd element to an int, which would give for example:

    List<Tuple<string, int, string>> tupleSpecialInt = new List<Tuple<string, int, string>>

The problem is that as this tuple has 3 elements, I don't see how to sort in ascending order this tuple according to its 2nd element.

How to sort in C# this tuple in ascending order based on its 2nd element (int)?


Thank you for your help.


Answers (1)