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);
|- 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? - AnatolFile.WriteAllLines? - user256147