-1

I have IP table with 2 columns: IP and % what is the best way to sort this table according IP precent ?

enter image description here

table type:

public List<string> statistics()
{

}
user979033
  • 2,656
  • 4
  • 23
  • 38
  • a List is only one data field. How are you storing 2 columns in one string? – hspain Mar 07 '12 at 16:26
  • so does each string contain `ipaddress {spaces} percent`? – Daniel A. White Mar 07 '12 at 16:26
  • 1
    Not enough information here. What are the types for your columns? Is the data coming from SQL? Are you using Entity Framework? Is this an in memory table? Is this coming from tab delimited text? – JamieSee Mar 07 '12 at 16:28

3 Answers3

0

Assuming your IpAddress column is a string and you just want that column value sorted by percentage descending (a lot of assumptions since your question is vague):

List<string> ips = IpTable.OrderByDescending(x=> x.Percentage)
                          .Select(x=> x.IpAddress)
                          .ToList();
BrokenGlass
  • 149,257
  • 27
  • 271
  • 318
0

You'll have to split the string into two columns, parse the second column into numbers and then orderby that column.

        return from line in statistics
               let split = line.Split('\t', '%')
               let percentage = double.Parse(split[1])
               orderby percentage
               select line;

In this code, you'll have to replace the splitting characters by whatever character(s) you have in each of your lines and replace the column I'm indexing as well, but the rest should work as you need.

Ani
  • 10,482
  • 3
  • 22
  • 42
  • after i typed this i received error in select line: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List – user979033 Mar 07 '12 at 19:08
  • Add a .ToList() at the end, if you want to assign the result to a list. :) – Ani Mar 07 '12 at 20:02
0

I think every entry in your list is a string - if not just ignore the following...

You first have to parse this into another format - don't know if this is true but I think there is a tab ('\t') between the Ip's and your percent, so you could start by doing something like

var stringTupleList = 
   myList.Select(s => s.Split('\t'))
         .Select(ar => new { Ip = ar[0], PercentString = ar[1] });

This gives you a list of anonymous types and you can continue like BrokenGlass answered:

var stringTupleList = 
   myList.Select(s => s.Split('\t'))
         .Select(ar => new { Ip = ar[0], PercentString = ar[1] })
         .OrderByDescending(t => t.PercentString)
         .Select(t => t.Ip)
         .ToList();
Carsten
  • 49,407
  • 9
  • 85
  • 111