C# 指定索引排序 (原)

  private void test(string[] sortkey_list, string[] list_temp)
        {

            //打开excel到dt;
            string[] strlist = { "4", "2", "3", "1" };

            string[] rowws;
            rowws = new string[] { "AA", "BB", "CC", "DD" };

            //第一行加入数组完成;
            Dictionary<int, string> dir = new Dictionary<int, string>();
            int index;
            //指定索引排序
            for (int k = 0; k < rowws.Length; k++)
            {
                if (strlist.Length == k) { dir.Clear(); break; }  //判断k超过 指定索引 数组的索引时; 终止

                index = Convert.ToInt32(strlist[k]); // 指定索引数组中 元素转换为整型

                if ((index - 1) == k) { continue; } //索引相同的值保持不变

                if ((index - 1) > k)
                {
                    dir.Add(k, rowws[k]);  //保存当前对象
                    rowws[k] = rowws[index - 1]; // 把索引指向的对象 赋值给当前对象
                }
                if ((index - 1) < k)
                {
                    dir.Add(k, rowws[k]);  // //保存当前对象
                    rowws[k] = dir[index - 1]; //字典取值
                    dir.Remove(index - 1); //删除元素
                }
            }

            //替换内容
            string[] strs = textBox2.Text.Split(' ');
            index = 0;
            for (int ii = 0; ii < strs.Length; ii++)
            {
                if (strs[ii] == "@")
                {
                    strs[ii] = rowws[index++];
                }
            }

            //合并字符串
            var result = string.Join(" ", strs);

        }
原文地址:https://www.cnblogs.com/enych/p/11536477.html