C#で行を入れ替える

個人的なメモです

        string[] _kugiri = { "\r\n" };
        string[] dm_list = { "グルコース", "HbA1c(JDS)", "HbA1c(NGSP)", "グリコアルブミン" };
        string[] shishitu_list = { "総コレステロール", "中性脂肪", "HDL-コレステロール", "LDLコレステロール", "計算LDL" };
        string[] tp_u_list = { "U-クレアチニン", "尿蛋白定量" };

        private void button1_Click(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                //リストを初期化
                List<string> sonota = new List<string>();
                List<string> dm = new List<string>();
                List<string> shishitu = new List<string>();
                List<string> tp_u = new List<string>();

                //クリップボードからリストを取得
                List<string> lines = new List<string>();
                lines = Clipboard.GetText().Split(_kugiri, StringSplitOptions.RemoveEmptyEntries).ToList();
                Regex rgx = new Regex(@"^\d+\s+");
                IEnumerable<string> result2 = 
                    lines.Where(s => Regex.IsMatch(s, @"^\d+"))
                    .Select(s =>
                    {
                        s = rgx.Replace(s, "");
                        return s;
                    });
                //データの振り分け
                foreach(string x in result2)
                {
                    Regex reg = new Regex(@"^(?<komoku>\S+)\s+");
                    Match m = reg.Match(x);
                    string komoku_name = m.Groups["komoku"].Value;
                    int switch_num = 0;
                    if (dm_list.Contains(komoku_name))
                    {
                        switch_num = 1;
                    }
                    if (shishitu_list.Contains(komoku_name))
                    {
                        switch_num = 2;
                    }
                    if (tp_u_list.Contains(komoku_name))
                    {
                        switch_num = 3;
                    }
                    switch (switch_num)
                    {
                        case 1:
                            dm.Add(x);
                            break;
                        case 2:
                            shishitu.Add(x);
                            break;
                        case 3:
                            tp_u.Add(x);
                            break;
                        default:
                            sonota.Add(x);
                            break;
                    }
                }
                string result = "";
                if (sonota.Count() != 0)
                {
                    result = String.Join("\r\n", sonota.ToArray());
                    result = result + "\r\n\r\n";
                }
                if (dm.Count() != 0)
                {
                    result = result + String.Join("\r\n", dm.ToArray());
                    result = result + "\r\n\r\n";
                }
                if (shishitu.Count != 0)
                {
                    result = result + String.Join("\r\n", shishitu.ToArray());
                    result = result + "\r\n\r\n";
                }
                if (tp_u.Count != 0)
                {
                    if (tp_u.Count == 2)
                    {
                        string cre_u_num = "";
                        string tp_u_num = "";

                        double cre;
                        double tp;

                        foreach (string x in tp_u)
                        {
                            if (Regex.IsMatch(x, "^U-クレアチニン"))
                            {
                                Regex reg = new Regex(@"^U-クレアチニン\s+(?<cre_urine>[0-9]*[.]*[0-9]*)");
                                Match m = reg.Match(x);
                                cre_u_num = m.Groups["cre_urine"].Value;
                            }
                            if (Regex.IsMatch(x, @"^尿蛋白定量"))
                            {
                                Regex reg = new Regex(@"^尿蛋白定量\s+(?<tp_urine>[0-9]*[.]*[0-9]*)");
                                Match m = reg.Match(x);
                                tp_u_num = m.Groups["tp_urine"].Value;
                            }
                        }

                        double.TryParse(cre_u_num, out cre);
                        double.TryParse(tp_u_num, out tp);

                        if (cre != 0 & tp != 0)
                        {
                            string answer;
                            answer = Math.Round(tp / cre, 2, MidpointRounding.AwayFromZero).ToString("0.00");
                            string output;
                            output = "TP/Cr(U) " + answer + " g/g.Cr";
                            result = result + String.Join("\r\n", tp_u.ToArray());
                            result = result + "\r\n" + output;
                        }
                    }
                    else
                    {
                        result = result + String.Join("\r\n", tp_u.ToArray());
                    }
                }
                Clipboard.SetText(result);
            }
        }