I need to sort it with bubble sort from text file!
I made with sorting letters in words but don't know how to sort strings in alphabetical order, like that:

post
new
thread

=

new
post
thread

mine code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\test.txt";
            FileStream fs = new FileStream(path, FileMode.Open);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            string text = sr.ReadToEnd();
            string[] Text = text.Split(' ', '\n');
            string[] outText = new string[Text.Length];

            for (int i = 1; i < Text.Length-1 ; ++i)
            {
                char[] ch = Text[i].ToCharArray();
                char[] cht = Text[i+1].ToCharArray();
                bool bol = true;
                while (bol)
                {
                    bol = false;
                    for (int j = 0; j < Math.Min(Text[i].Length, Text[i + 1].Length); ++j)
                    {
                        if (ch[j] > cht[j])
                        {
                            string tmp = Text[i];
                            Text[i] = Text[i + 1];
                            Text[i + 1] = tmp;
                            bol = true;
                        }
                    }
                }
                Console.WriteLine(Text[i]);
//sorting letters
                /*while (bol)
                {
                    bol = false;
                    for (int j = 0; j < ch.Length - 1; j++)
                    {
                        if (ch[j] > ch[j + 1])
                        {
                            char tmp = ch[j];
                            ch[j] = ch[j + 1];
                            ch[j + 1] = tmp;
                            bol = true;
                        }
                    }
                }
                outText[i] = new string(ch);
                Console.WriteLine(outText[i]);*/
            }

            Console.Write("\nPress any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

Recommended Answers

All 3 Replies

Use the "CompareTo()" method of the string class. It compares two strings and returns an integer, thusly:

int iCmp = Text[i].CompareTo(Text[i+1]);
if (iCmp < 0)
{
    // Text[i] is "less than" Text[i+1]
}
else if (iCmp > 0)
{
    // Text[i] is "greater than" Text[i+1];
}
else
{
    // Strings are equal
}

Use the "CompareTo()" method of the string class. It compares two strings and returns an integer, thusly:

int iCmp = Text[i].CompareTo(Text[i+1]);
if (iCmp < 0)
{
    // Text[i] is "less than" Text[i+1]
}
else if (iCmp > 0)
{
    // Text[i] is "greater than" Text[i+1];
}
else
{
    // Strings are equal
}

Use the Compare method: http://msdn.microsoft.com/en-us/library/84787k22.aspx

thanks, will try

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.