-1

I am working on a program that sorts dictionary words by length. The current code outputs the results. I am having trouble saving the results as a text file.

Current Code

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
    // Initialize a List of strings.

    string filePath = "text.txt";
    List<string> linesList = new List<string>();
    string[] fileContent = System.IO.File.ReadAllLines(filePath);
    linesList.AddRange(fileContent);
    // Send the List to the method.
    foreach (string s in SortByLength(linesList))
    {
        Console.WriteLine(s);
    }
 System.IO.File.WriteAllLines("solution.txt",sorted);
}

static IEnumerable<string> SortByLength(IEnumerable<string> e)
{
    // Use LINQ to sort the array received and return a copy.
    var sorted = from s in e
                 orderby s.Length ascending
                 select s;
    return sorted;
}
}
Jane
  • 3
  • 1
  • 4
  • What exactly is the trouble? Is there an error message? – Jasen Mar 06 '18 at 03:31
  • I don't think that question is exactly a duplicate, because the poster there is asking about reading and writing strings, not `string[]` or `IEnumerable`. It doesn't mention `File.WriteAllLines()` until this answer: https://stackoverflow.com/a/34678050/226781 – asherber Mar 06 '18 at 03:36
  • It's similar but not quite. I need to use WriteAllLine function. I edited the code with error I am getting. – Jane Mar 06 '18 at 04:01
  • @asherber So... still answers this one. In my view second answer (with `StreamWriter`) is actually way more suitable for code in the original version of the post as only small change would be needed to convert `Console.WriteLine` to `writer.WriteLine`... On other hand after the edit it is clear that problem is not related to writing to file at all but rather to scope of values/returning results from function. But that is not what question asks *as written*. More [edit] needed to reopen. – Alexei Levenkov Mar 06 '18 at 05:00
  • @Jane "I edited the code with error I am getting" - there is no error shown in the post. If you are getting particular error - make sure to write it in the question (preferably using `>` quote formatting), if you have particular requirement (like your assignment asks for particular method to be used - "I need to use WriteAllLine function") - write it out. People answering your question and future visitors have no idea what assignments you have and what requirements need to be meet without you spelling them out in the question. – Alexei Levenkov Mar 06 '18 at 05:02

1 Answers1

1

I think you want File.WriteAllLiness(filePath, SortByLength(linesList));

Note that your code can be simplified, because there's no reason to initialize linesList as you are. You can just do:

string[] fileContent = File.ReadAllLines(filePath);
var linesList = SortByLength(fileContent);
foreach (string s in lineslist)
{
   //...
}
File.WriteAllLines(filePath, linesList);
asherber
  • 2,508
  • 1
  • 15
  • 12
  • Thank you! This is exactly what I was looking for. I was using File.WriteAllLiness(filePath, sorted) instead of what you suggested. Needless to say, I am completely new to C#. – Jane Mar 06 '18 at 04:04
  • I'm guessing that you were getting a compilation error, since the variable `sorted` is not in scope in `Main()` – it only exists inside of `SortByLength()`. – asherber Mar 06 '18 at 04:06