搜索和排序多个文件

时间:2016-04-17 22:08:22

标签: c#

需要搜索6个txt文件,然后将它们一起排序。任何开始的想法?

1 个答案:

答案 0 :(得分:1)

如果我假设您的数据已按升序排序,您的用户输入将为年和月。例如,他们希望查看2014年7月的天气数据,然后您可以使用数组的索引作为相同的行号指示符。请查看以下代码:

static void Main(string[] args)
{
    string yearInput = "2014"; // User input
    string monthInput = "July"; // User input

    string[] Year = { "2013", "2014", "2014", "2014", "2014", "2014", "2015" };
    string[] Month = { "July", "July", "August", "September", "October", "November", "December", "January" };
    string[] rain1 = { "0", "1", "2", "3", "4", "5", "6", "7" };
    string[] sun1 = { "0a", "1a", "2a", "3a", "4a", "5a", "6a", "7a" };

    var yearIndex = Array.FindIndex(Year, year => year == yearInput);
    var monthIndex = Array.FindIndex(Month, yearIndex, month => month == monthInput);

    var outputRain = rain1[monthIndex]; // The corresponding rain for the input
    var outputSun = sun1[monthIndex]; // The corresponding sun for the input
}

Array.FindIndexMSDN开始,到searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within an Array or a portion of it.

从那里,我们可以找到指定年份的第一次出现的索引,并将其用作startIndex来搜索指定月份的索引,以便跳过2013年并从2014年开始操作。

根据我的假设得到monthIndex之后,我们可以将其视为行号并获取天气数据。希望它有所帮助。

相关问题