我怎样才能让它自动化?我如何对List中的每组索引进行排序?

时间:2013-10-28 18:08:26

标签: c# winforms

这是我在新课程中使用的方法:

public ExtractImages(List<string> FirstTags, List<string> LastTags, List<string> Maps, string LocalFileDir, string UrlsDir)
        {
            localdir = LocalFileDir;
            counter = 0;
            imagesSatelliteUrls = new List<string>();
            imagesRainUrls = new List<string>();
            int startIndex = 0;
            int endIndex = 0;
            int position = 0;
            for (int i = 0; i < Maps.Count; i++)
            {

                    string startTag = FirstTags[i];
                    string endTag = LastTags[i];
                    startIndex = Maps[i].IndexOf(startTag);
                    while (startIndex > 0)
                    {

                        endIndex = Maps[i].IndexOf(endTag, startIndex);
                        if (endIndex == -1)
                        {
                            break;
                        }
                        string t = Maps[i].Substring(startIndex, endIndex - startIndex + endTag.Length);
                        if (i == 0)
                        {
                            imagesSatelliteUrls.Add(t);
                            counter++;
                        }
                        if (i == 1)
                        {
                            imagesSatelliteUrls.Add(t);
                        }
                        position = endIndex + endTag.Length;
                        startIndex = Maps[i].IndexOf(startTag, position);

                    }
                    if (i >= 0)
                    {
                        imagesSatelliteUrls.Insert(0, "Group 1");
                    }
                    if (i == 1)
                    {
                        imagesSatelliteUrls.Insert(counter, "Group 2");
                    }
                    //imagesSatelliteUrls = imagesSatelliteUrls.OrderBy(q => q).ToList();
            }
        }

最后,作为List的imagesSatelliteUrls包含例如70个索引。 例如:

index[0] "Group 1"
index[1] some link here ... http...
index[2] some link here ... http...
.
.
.
index[30] some link here ... http...
.
.
.
.
index[45] "Group 2"
index[46] some link here ... http...
.
.
.
.
index[70] some link here ... http...

变量Maps包含7个索引。 所以会有7个迭代/循环。 我需要以某种方式让它自动,所以它会添加一个新的字符串,如:“组”+我 对于每组链接。

我可以继续重置变量计数器或使用另一个int变量并在if(i == 1)中计算它 然后在if(i == 2)

然后制作if(i == 2)then imagesSatelliteUrls.Insert(newCounter,“Group 2”);

但是为每个循环编写一个新的IF语句,我怎样才能使它全部自动化? 因此,在每个Maps迭代/循环中,它将添加一个新的“Group”+ i

接下来是我现在不使用的这一行:

imagesSatelliteUrls = imagesSatelliteUrls.OrderBy(q => q).ToList();

如果我现在将使用此行,它将把所有组放在列表的开头。 我需要以某种方式对每个组的索引(链接)进行排序。而不是排序所有List imagesSatelliteUrls。

1 个答案:

答案 0 :(得分:2)

如果您想为每个i添加每个组,那么您可以执行以下操作:

for (int i = 0; i < Maps.Count; i++) {
  //must place this at the very beginning of your loop
  imagesSatelliteUrls.Add("Group " + (i+1));
  //....
}