按自定义顺序对字符串数组进行排序 [英] Sort String array in custom order

查看:223
本文介绍了按自定义顺序对字符串数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆