0

The following example is from a msdn article on Sorting or Filtering Text Data by Any Word or Field (LINQ) (C#). I would like to do the same thing, but the text file I have is not a .csv file and does not have commas as delimiters and I must specify start and end position for the field/Column.

    public class SortLines
    {
        static void Main()
        {
            // Create an IEnumerable data source
            string[] scores = System.IO.File.ReadAllLines(@"../../../scores.csv");

            // Change this to any value from 0 to 4.
            int sortField = 1;

            Console.WriteLine("Sorted highest to lowest by field [{0}]:", sortField);

            // Demonstrates how to return query from a method.
            // The query is executed here.
            foreach (string str in RunQuery(scores, sortField))
            {
                Console.WriteLine(str);
            }

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }

        // Returns the query variable, not query results!
        static IEnumerable<string> RunQuery(IEnumerable<string> source, int num)
        {
            // Split the string and sort on field[num]
            var scoreQuery = from line in source
                             let fields = line.Split(',')
                             orderby fields[num] descending
                             select line;

            return scoreQuery;
        }
    }

/* Output (if sortField == 1):
   Sorted highest to lowest by field [1]:
    116, 99, 86, 90, 94
    120, 99, 82, 81, 79
    111, 97, 92, 81, 60
    114, 97, 89, 85, 82
    121, 96, 85, 91, 60
    122, 94, 92, 91, 91
    117, 93, 92, 80, 87
    118, 92, 90, 83, 78
    113, 88, 94, 65, 91
    112, 75, 84, 91, 39
    119, 68, 79, 88, 92
    115, 35, 72, 91, 70
 */

How do I go about sorting the above example when there are no delimiters in the text file. For instance, removing all the comma delimiters of the first line from the above example we have: 11699869094 so the score column is located at start location 3 and end location 4, which gives us the value 99. How do I specify the start and end location for a field/column to be sorted.

Eric J.
  • 139,555
  • 58
  • 313
  • 529
ptn77
  • 463
  • 2
  • 7
  • 17

1 Answers1

1

You can use string.Substring() in your ordering condition. I'm more familiar with the Lambda syntax for Linq.

var scoreQuery = source.OrderByDescending(line => line.Substring(3, 2));

Note that this assumes that you want the offset 3 from the string. If you want position 3, use 4 instead (Substring is 0-based). The 2 indicates the number of characters you want to use for ordering purposes.

Here's an attempt at the same solution using the query syntax. Slight modifications may be needed

        var scoreQuery = from line in source
                         orderby line.Substring(3, 2) descending
                         select line;
Eric J.
  • 139,555
  • 58
  • 313
  • 529
  • If I wanted to add an additional Column, would I be able to have it sort as follows: var scoreQuery = from line in source orderby line.Substring(3, 2) and line.Substring(5, 2) descending select line; – ptn77 Jun 15 '16 at 23:01
  • `var scoreQuery = from line in source orderby line.Substring(3, 2) descenting, line.Substring(5, 2) descending select line;` See http://stackoverflow.com/questions/298725/multiple-order-by-in-linq – Eric J. Jun 16 '16 at 00:38