按自定义顺序对字符串数组进行排序 [英] Sort String array in custom order
问题描述
我想按固定顺序对一组固定的字符串集进行排序,这些字符串集例如文本文件",图像文件",音频文件",视频文件",应用程序文件",其他文件"我已经提到过.
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";
这篇关于按自定义顺序对字符串数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!