110,539
社区成员
发帖
与我相关
我的任务
分享
Dictionary<string,int> NewLengths = new Dictionary<string,int>();
for (int n = 0; n < tableArray.Length - 1; n++)
{
for (int u = 0; u < TableLengths.Count; u++)
{
if (TableLengths[u].Name == tableArray[n])
{
NewLengths.Add(TableLengths[u].Name,TableLengths[u].Length);
}
}
}
var list = (from d in Lengths orderby d.Value ascending select d).ToList();//对数据字典进行排序
这样默认是按升序排序的
class Program
{
static int keySelector(KeyValuePair<string, int> entry)
{
return entry.Value;
}
static void Main()
{
Dictionary<string, int> vals = new Dictionary<string, int>();
vals.Add("asp.net", 3);
vals.Add("vb.net", 2);
vals.Add("html5", 1);
IOrderedEnumerable<KeyValuePair<string, int>> result = vals.OrderBy(keySelector);
foreach (var item in result)
Console.WriteLine("{0}:{1}", item.Key, item.Value);
}
}
string[] itemList =new string[] { "x=10", "y=5", "k=4", "r=9" };
int flag = 1;
int i, j;
int itemCount = itemList.Length;
string itemTemp;
for (i = 1; i < itemCount && flag == 1; i++)
{
flag = 0;
for (j = 0; j < itemCount - i; j++)
{
string itemfore = itemList[j];
int countfore = Convert.ToInt32(itemfore.Substring(itemfore.IndexOf('=') + 1, itemfore.Length - (itemfore.IndexOf('=') + 1)));
string itemback = itemList[j + 1];
int countback = Convert.ToInt32(itemback.Substring(itemback.IndexOf('=') + 1, itemback.Length - (itemback.IndexOf('=') + 1)));
if (countfore < countback)
{
flag = 1;
itemTemp = itemList[j];
itemList[j] = itemList[j + 1];
itemList[j + 1] = itemTemp;
}
}
}
return itemList;
}