我想要对人名列表进行排序,但其他联系人列表也必须进行排序,以保持相同的索引,以便联系人与姓名相对应。我有这样一门课:
List<string> name = new List<string>();
List<string> cellphone = new List<string>();
public void setName(string value)
{
name.Add(value);
}
public void setCellphone(string value)
{
cellphone.Add(value);
}
public List<string> getNames()
{
return name;
}
public List<string> getCellphones()
{
return cellphone;
}现在,我想对它们进行排序;
例如:
列表1:-约瑟夫-安娜
列表2:- +351912345678 - +351931234567
结果必须是:
列表1:--安娜-约瑟夫
列表2:- +351931234567 - +351912345678
发布于 2013-07-03 06:24:02
您应该使用单个类来保存一个人的信息。
现在来看问题:Zip + OrderBy +2 *( Select + ToList)可以按同样的顺序给出排序列表。类似于:
var pairs = name.Zip(cellphone, (name, phone)=> new {name, phone})
.OrderBy(item => item.name);
name = pairs.Select(item => item.name).ToList();
cellphone = pairs.Select(item => item.phone).ToList();发布于 2013-07-03 06:21:57
你应该使用字典,而不是2个列表。查看此处:http://www.dotnetperls.com/dictionary
https://stackoverflow.com/questions/17436767
复制相似问题