-3

I have a list

List<string[]> listname = ...

The list looks like this:

[string][string][string]

I want to sort the list by second string.

The second string is a number presented as a string, i want to keep it that way, i need it like this.

I want the numbers to be in Increasing order.

Example for data:

["RDP"]["3389"]["TCP"] 
["HTTP"]["80"]["TCP"]
["HTTPS"]["443"]["TCP"]

I want to sort by the post number.

In this example "RDP" will become the last one.

How can I do so?

Or Yagoda
  • 55
  • 1
  • 8

2 Answers2

0
var sortedList = listname.OrderBy(l => int.Parse(l[1]));

This assumes that the second entry in each array is parsable as int. If you're not sure about that, you'd need to add some checks and maybe use int.TryParse.

germi
  • 4,628
  • 1
  • 21
  • 38
0

You can refer to appropriate index of an array in OrderBy:

var l = new List<string[]>() { 
                                new string[] { "a", "b", "c" }, 
                                new string[] { "b", "a", "c" }, 
                                new string[] { "c", "c", "c" }, 
                                };
var s = l.OrderBy(c => c[1]).ToList();
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75