按自定义顺序对字符串数组进行排序

问题描述:

我想按固定顺序对一组固定的字符串集进行排序,这些字符串集例如文本文件",图像文件",音频文件",视频文件",应用程序文件",其他文件"我已经提到过.

I want to sort a fixed set of strings like "Text files", "Image files", "Audio files", "Video files", "Application Files", "Other files" in a string array in the same order I have mentioned.

Example1,如果我的字符串数组输入是这样

Example1, if my string array input is like this

inputval[0] = "Other files";
inputval[1] = "Image files";
inputval[2] = "Text files";

我的输出数组应该具有这样的值

my output array should have values like this

outputval[0] = "Text files";
outputval[1] = "Image files";
outputval[2] = "Other files";

示例2,如果我的字符串数组输入是这样

Example 2, if my string array input is like this

inputval[0] = "Application files";
inputval[1] = "Image files";
inputval[2] = "Video files";

我的输出数组应该具有这样的值

my output array should have values like this

outputval[0] = "Image files";
outputval[1] = "Video files";
outputval[2] = "Application files";

请有人帮我实现这一目标

Please can somebody help me in achieving this

因为您想要的内容并不十分清楚.因此,我已经考虑到inputval中将没有重复.

Since not very much is clear from what you want. So i have taken into consideration that there will be no repetitions in inputval.

string[] fixed_array =  { "Text files", "Image files", "Audio files", 
                        "Video files", "Application Files", "Other files" };

让我们说

inputval[0] = "Other files";
inputval[1] = "Image files";
inputval[2] = "Text files";

执行此操作

string[] outputval =
          fixed_array.Select(x => inputval.Contains(x) ? x : "-1")
                     .Where(x => x != "-1").ToArray();

所以outputval将是

outputval[0] = "Text files";
outputval[1] = "Image files";
outputval[2] = "Other files";