Does anyone remember how ms dos had a sort program that you could sort the rows in a huge text document based on just one character by its position from the left margin?

I am trying to recreate that effect in a notepad clone. I am sort of new to C# but not to programming, a friend of mine works as a repair man at a factory and the jobs his machines run are in text database files, that each new line is a row of data, one of the values is a number representing rather or not that row's data was ran and accepted, I would like to write a simple program sort function that would sort the rows by that one value so all the unaccepted rows would all go to the top, without affecting the text in the row. so that that text could be easily cut and pasted in a new document to be ran by the machines again.

sorry for the long back story,

I found some code that is suppose to read a file and sort it using a list view, but that's not exactly what I am going for, and I couldn't get it to work right.

I am willing to do whatever necessary but so far trial and error just hasn't done anything for me.

thanks,
DiamondDrake

I have Gotten this far, I can put each line in order alphanumerically, but its the entire line, If anyone could point out how I could make the lines order alphanumerically by a specific character from the left margin, or at least point me in the right direction I would appreciate it. Thanks.

List<string> myLines = new List<string>();
            using (StringReader r2 = new StringReader(txtMain.Text))
            {
                string txtLine;
                while ((txtLine = r2.ReadLine()) != null)
                {
                    myLines.Add(txtLine);
                }
            }
            myLines.Sort();
            using (StringWriter sw = new StringWriter())
            {
                foreach (string s in myLines)
                {
                    sw.WriteLine(s);
                }
                txtMain.Text = sw.ToString();
            }

I dont see why you'd be having a problem the following

List<String> lines = textBox1.Text.Split('\n').ToList();
            lines.Sort();
            textBox2.Text = String.Join("\n", lines.ToArray());

With

"a cat
a bat
a mat"

put into textbox1, sorted it correctly

It does work, but it sorts by the entire line, Lets say I have a txt file like this:

110125614, 55614, 1289489 685481 685489 3 35478956
694894894, 85989, 7478498 651856 547878 1 68994898
547846514, 35247, 5248948 658748 848655 2 68489894
524754754, 32348, 5856445 489585 598788 0 91156419

I would like the sort function to ignore all the characters except one a specific number of characters from the margin for example the bold character in my example above so that the output would be:

524754754, 32348, 5856445 489585 598788 0 91156419
694894894, 85989, 7478498 651856 547878 1 68994898
547846514, 35247, 5248948 658748 848655 2 68489894
110125614, 55614, 1289489 685481 685489 3 35478956

see, I would like the lines not to change, just be ordered my a certain character in each line and I'm lost at how to go about it.

As long as the lines are unique you would need to either use a dictionary and sort an array/list of those characters being the key to the dictionary, OR, you need to write your own sort routine.

i looked into using a dictionary, but I couldn't seem to taper its usage to match my intentions. and I don't know where to begin on writing my own sort routine. I am working on a method to compare substring values in an array containing all the lines. i will post some code when I have something that makes any sense at all.

any pointers or suggestions would be appreciated.
Thanks

I found the answer,
basically I created 2 arrays, I read each line of text from my text box, copied it in its entirety into my first array, then I used a substring to find just the chosen column and copied that character to the 2nd array, then i called the Array.sort(array, array); method sorting the substring array, then wrote the array with the full lines to the text box not nice and sorted by the selected substing of each line, simple, Very efficient, and perfect.

Thanks for your suggestions LizR, but I guess I just needed to better investigate the usage of the methods I already knew about.

This thread is sloved.

Is it possible for you to post the code you used?

I am struggling to do this as we speak, but can't get it to work.

Thanks in advance.

Schokbreker

Is it possible for you to post the code you used?

I am struggling to do this as we speak, but can't get it to work.

Thanks in advance.

Schokbreker

I found many ways to get the lines to the arrays, but the way that seemed to work the best for my application was to create some string lists add the values to the lists and then convert them to arrays sort the first using the 2nd as a key. then send them back to your text field or where ever you want them.

Its a TON of code that i worked very hard on, and it works great for 100,000 lines or so, but get over 250,000 lines and you tend to get memory errors.

I will give you a simple fly by version

try
            {
//create some string lists to hold your lines

                List<string> mySortLines = new List<string>();
                List<string> mySortLines2 = new List<string>();

                //declare a variable to hold the lengh of the first line
                int int4length = 0;
                int X = 0;
                
//create a string reader to read in the lines of text from RTB (txtMain)
                using (StringReader r3 = new StringReader(txtMain.Text))
                {
//create some vars to hold the selected text (on the first line) to show what column to sort by.

                    string txtSortLine;
                    int txtSelectedStart = txtMain.SelectionStart;
                    int txtSelectedLength = txtMain.SelectionLength;
                    string txtSortcharacter = string.Empty;
//if no text is selected, abort and show error message
                    if (txtSelectedLength == 0)
                    {
                        MessageBox.Show("you must select characters from the first line of text", "Sort Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
//do the magic, create loop to run each line of text
                    while ((txtSortLine = r3.ReadLine()) != null)
                    {

                        //set int4lenght var

                        if (X < 1)
                        {
                            int4length = txtSortLine.Length;
                            X++;

                            //check if text is selected on the first line
                            if (int4length < txtSelectedStart)
                            {
                                MessageBox.Show("you must select characters from the first line of text", "Sort Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return;
                            }
                        }

                        //check if line contains entire selected coulmns 
                        if (txtSortLine.Length >= (txtSelectedStart + txtSelectedLength))
                        {

                            mySortLines.Add(txtSortLine);
                            mySortLines2.Add(txtSortLine.Substring(txtSelectedStart, txtSelectedLength));
                                


                        }
                        //check if lines are shorter than selected columns if so sort as blank
                        if (txtSelectedStart + 1 > txtSortLine.Length)
                        {
                            mySortLines.Add(txtSortLine);
                            mySortLines2.Add("");
                                  ProgressBar.Value += 1;
                        }
                        //need to sort for lines that contain part of selected columns
                        if (txtSortLine.Length >= (txtSelectedStart + 1) && txtSortLine.Length < (txtSelectedStart + txtSelectedLength))
                        {
                            mySortLines.Add(txtSortLine);
                            mySortLines2.Add(txtSortLine.Substring(txtSelectedStart, txtSortLine.Length - (txtSelectedStart)));
                                  ProgressBar.Value += 1;
                        }


                    }

//convert lists to arrays, then sort lines, by column keys
                    string[] sortArray = mySortLines.ToArray();
                    string[] sortArray2 = mySortLines2.ToArray();
                    Array.Sort(sortArray2, sortArray);

//little cleaning
                    mySortLines = null;
                    mySortLines2 = null;
                    sortArray2 = null;
//create a string writer and put the sorted text back to the RTB
                    using (StringWriter sw2 = new StringWriter())
                    {
                        foreach (string s in sortArray)
                        {
                            sw2.WriteLine(s);
                        }
                        txtMain.Text = sw2.ToString();
                        txtMain.Modified = true;


                        sortArray = null;


                    }

                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error");
            }
        }

that should be all the magic right there, took me weeks of asking all the wrong questions on 4 forums before I finally found while searching the usage of sorting arrays by a key. Noone could help me, I finally just figured it out. Its sloppy I know, but it works, there were no programs for windows that does this, I had to be the man to fix that. I have an application almost ready for release called sort pad it is a notepad like app that has tons of sorting functions, this being the most unique one.

Use it wisely,

DiamondDrake

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.