如何使其自动?以及如何对列表中的每组索引进行排序
本文关键字:索引 排序 何使其 列表 | 更新日期: 2023-09-27 18:37:14
这是我在新类中的方法:
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();
}
}
最后,图像卫星网址是列表包含例如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...
变量地图包含 7 个索引。所以会有 7 个迭代/循环。我需要以某种方式使其自动,以便它会添加一个新字符串,例如:"组"+ i对于每组链接。
我可以继续重置变量计数器或使用另一个 int 变量并将其计算在 if (i == 1) 中然后在 if (i == 2)
然后使if(i == 2)然后imagesSatelliteUrls.Insert(newCounter,"Group 2");
但是,与其为每个循环编写一个新的 IF 语句,不如如何使其全部自动?所以在每个 Maps 迭代/循环中,它将添加一个新的"组"+ i
接下来是我现在不使用的这一行:
imagesSatelliteUrls = imagesSatelliteUrls.OrderBy(q => q).ToList();
如果我现在使用此行,它将把所有组放在列表的开头。我需要以某种方式对每个组的索引(链接)进行排序。而不是对所有列表图像卫星网址进行排序。
如果你想为每个i
添加每个组,它非常简单,你可以做这样的事情:
for (int i = 0; i < Maps.Count; i++) {
//must place this at the very beginning of your loop
imagesSatelliteUrls.Add("Group " + (i+1));
//....
}