Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Iam trying to sort records after writing it to text file with binary writer but program only sort from 0 to 9 when i put the id=10 program consider it 1 and put it after 1
this the code where i write,save andsort

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
    BinaryWriter bw = new BinaryWriter(File.Open(Class1.filename, FileMode.Open, FileAccess.Write));
    int length = (int)bw.BaseStream.Length;

    if (length == 0)
    {
        bw.Write("ID :" + int.Parse(textBox2.Text));
        textBox3.Text = textBox3.Text.PadRight(9);
        bw.Write("Name :" + textBox3.Text.Substring(0, 9));

        length += Class1.rec_size;
    }
    else
    {
        bw.BaseStream.Seek(length, SeekOrigin.Begin);
        bw.Write("ID :" + int.Parse(textBox2.Text));
        textBox3.Text = textBox3.Text.PadRight(9);
        bw.Write("Name :" + textBox3.Text.Substring(0, 9));
    }

    textBox2.Text = textBox3.Text  = "";
    MessageBox.Show("Data is saved Successfully");

    bw.Close();

    string inFile = Class1.filename;
    string outFile = Class1.filename;

    var contents=File.ReadAllLines(inFile);
    Array.Sort(contents);
    File.WriteAllLines(outFile, contents);
}
Posted
Updated 25-Apr-17 2:55am
v2
Comments
[no name] 25-Apr-17 8:27am    
Because you are sorting strings, that is the expected behavior. If you want a numeric sort, you have to convert the string your are trying to sort to a number.
Member 13151067 25-Apr-17 8:30am    
How can i sort it numeric sort?
Afzaal Ahmad Zeeshan 25-Apr-17 8:47am    
Convert that number to integer instead of string.
Alan N 25-Apr-17 9:54am    
Other have given you good ideas for sorting numeric strings.
Something that has not been mentioned is that BinaryWriter writes length prefixed strings to the file. Such files may not read properly with File.ReadAllLines and probably won't display correctly in Notepad. To create a pure text file you should use System.IO.StreamWriter.

You have to format the numbers with a fixed length and leading zeroes so that they can be sorted as text.

One option is to use the Int32.ToString Method (String) (System)[^]:
C#
bw.Write("ID :" + Int32.Parse(textBox2.Text).ToString("D11"));

But from my point of view your code does not write line feeds to the file so that File.ReadAllLines will return an array with only one string. Or do I miss something?
 
Share this answer
 
When you compare strings, the comparison works by looking at each character one-by-one until it finds a single character which is not the same in both strings. the entire result of the comparison is then based on the difference between these two characters.
So when you sort strings containing numeric values, the sort order is:
1
10
11
12
13
...
18
19
2
20
21
...

In order to compare numeric values correctly, you would either need to left-pad all the number with zeros so they are the same length when you write the file:
00001
00002
00003
...
00009
00010
00011
...
Or parse the string value to a number and compare those, using int.TryParse on the relevant substring from the file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900