1

I've been using Array.Sort() to sort an array of string, but for some reason it is keeping the first element of the array and outputting not in order.

    private void quicksort1_Click(object sender, EventArgs e)
    {


        String[] parts = new String[1000];

        //System.IO.StreamReader file = new System.IO.StreamReader(textBox1.Text);
        System.IO.StreamWriter output = new System.IO.StreamWriter("OUTPUT.txt");

        parts = File.ReadAllLines(textBox1.Text);
        foreach (string s in parts)
        {
                Array.Sort(parts);
                parts.Equals(s);
                output.WriteLine(s);
                counter++;

        }
        output.WriteLine("There were" + " "  + counter + " " + "lines read in.");

        output.Close();

I was just wondering if there was a possible solution to where Array.Sort() would sort the first element as well as the others.

4
  • 4
    Why do you sort the array every iteration? Commented May 23, 2013 at 16:53
  • Why do you think it is NOT sorting the first element? Commented May 23, 2013 at 16:56
  • I think @Vipar is on to something. Sorting the parts object when it is being referenced by the foreach loop may have some unintended side-effects. Have you tried moving the Array.Sort(parts); statement to before the start of the loop? Commented May 23, 2013 at 17:00
  • 1
    The problem is that @Stock is pulling the value out before the sort occurs, so the first print is the value from the unsorted array, but after that, it's always going to be taking the value from the array after sort (but it's doing a lot of other things it shouldn't). Commented May 23, 2013 at 17:13

3 Answers 3

4

Right now, you're sorting the entire set once per line. Instead, you can just sort the lines once in advance of your loop.

In your current code, the first line appears unsorted because you're fetching it prior to the sort:

foreach (string s in parts) // Getting s from the original array
{
   Array.Sort(parts);  //Sorting array
   // s is unchanged - at this point its still the first element from the original array

Instead, sort up front. Try this:

private void quicksort1_Click(object sender, EventArgs e)
{
  using (var output = new System.IO.StreamWriter("OUTPUT.txt"))
  {
    string[] parts = File.ReadAllLines(textBox1.Text);
    Array.Sort(parts);
    foreach (string s in parts)
    {
        output.WriteLine(s);
    }

    output.WriteLine("There were {0} lines read in.", parts.Length);
  }

    }

Note that there is also no need for a counter variable here, and switching the code to use the using statement simplifies the logic a bit.

Sign up to request clarification or add additional context in comments.

6 Comments

Now I'm trying to do this with integers, but the output would be something like "1, 10, 11, 12, 2, 3, 4" how would I get it to sort the values? Sorry, I'm relatively new to C# and programming.
@Stock Convert it to integers up front. LINQ can be good for this, ie: var partNumbers = File.ReadAllLines(textBox1.Text).Select(line => Convert.ToInt32(line)).OrderBy(i => i); That will return the numbers already sorted
I'm still a little confused as to how to output this to a file, sorry to bother.
@Stock: File.WriteAllLines("OUTPUT.txt", partNumbers.Select(i => i.ToString()));
@Stock The above will give that if there is a line that isn't a number (and can't be parsed into an int). THat includes blank lines (you'd have to handle them separately)
|
2

This needs to be outside of the loop (before the loop that is): Array.Sort(parts);

By the time you have done the sort, you're already accessing the first element. The first element is getting sorted in the Array, you've just accessed it in it's previous position in the enumeration.

Comments

0

You are outputting the first line using the line output.WriteLine(s). The problem is that s is retrieved from your list before the list is sorted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.