In the file there are lines like:

294|ab 124h|b4 903|ac 626|ad 2364|ag 913a|it 390q|a8 

there is a line by which it is necessary to sort, that is, string digits="abcdefghijklmnopqrstuvwxyz0123456789"; but how to make the lines in txt sorted alphabetically "digits", but after | icon. If the order of numbers changes, it means that what comes before "|" mark ... if these numbers are after | repeated, so they go in a row .. after sorting should be like this:

 294|ab 903|ac 626|ad 2364|ag 390q|a8 124h|b4 913a|it 

after | the character will be a string, what is the length anyway, everything that is after the | sort alphabetically

  • 2
    read the file, for each line make split by | - let it be what | will be the value, and then what after - the key, and put in the dictionary, sort the dictionary and overwrite the file? - Anatol
  • where is the problem from? - Anatol
  • @Anatol "but what after - the key" - what key can I find out? : / - user256147
  • @Tode Checked my version? There all this is provided, only instead of reading and writing to the file, I used richTextBox1 for clarity. Changing to read and write the file, get what you want. - koshe
  • @koshe instead of richTextBox1 need to register File.WriteAllLines ? - user256147

2 answers 2

First create a class:

 class Line { public string Text { get; set; } public string Fragment { get; set; } } 

and function:

 private string getFragment(string s,string digits) { if (string.IsNullOrEmpty(s)) return string.Empty; var t = s.Split('|'); if (t.Count() == 1) return string.Empty; s = t[t.Count() - 1]; for (int i = 0; i < digits.Length; i++) { s = s.Replace(digits[i], (char)i); } return s; } 

The function takes the text after the "|" and changes to sort by digits. After that, click on the button:

 private void Button1_Click(object sender, EventArgs e) { string digits = "0123456789"; var list = new List<Line>(); var lines = File.ReadAllLines("filename"); foreach (var s in lines) { list.Add(new Line { Text = s, Fragment = getFragment(s,digits) }); } lines = list.OrderBy(r => r.Fragment).Select(x => x.Text).ToArray(); File.WriteAllLines("filename", lines); } 

If you just sort by alphabetic, you can:

 class Line { public string Text { get; set; } public string Fragment { get { if (Text.Contains("|")) return Text.Split('|')[1]; else return string.Empty; } } } var list = new List<Line>(); var lines = File.ReadAllLines("filename"); foreach (var s in lines) { list.Add(new Line { Text = s}); } lines = list.OrderBy(r => r.Fragment, StringComparer.OrdinalIgnoreCase).Select(x => x.Text).ToArray(); File.WriteAllLines("filename", lines); 
  • Oh .. Why is there so much code? - Qwertiy
  • In general, it is interesting - keep a plus sign. Though I would drive getFragment into the Line constructor. - Qwertiy
  • @Qwertiy Fair remark, thanks. - koshe

In general, the approach described by koshe - the creation of a class-model - is correct.

But if the data from the file is not used anywhere else and the whole task is reduced only to sorting it, then you can do this:

 var lines = File.ReadAllLines("test.txt"); var sorted = lines.OrderBy(line => int.Parse(line.Split('|')[1])); File.WriteAllLines("test2.txt", sorted); 
  • Here is what the Unhandled "System.FormatException" type exception writes to mscorlib.dll Additional Information: The input string had the wrong format. - user256147
  • I have no buttons need console app. to sort and rewrite the same file - user256147
  • @Tode - This code works on the data set that is given in the question. There are no buttons in this code. - Alexander Petrov
  • He gives me an error .. - user256147
  • @Tode - I can only repeat: this code works on the data set that is given in the question. Show real data. - Alexander Petrov