1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
| // 这是C#代码
// Dictionary 与 List 位于 System.Collections.Generic 命名空间下
string[] StringSort(string[] source)
{
int[] sortKeys(int[] src) // 这里是用于排序字典的键的
{
int most;
int mostIndex = 0;
int temp;
if (src.Length > 1)
{
for (int i = 1; i < src.Length; i++)
{
most = src[i];
for (int j = i; j < src.Length; j++)
{
if (src[j] < most)
{
most = src[j];
mostIndex = j;
}
}
if (most < src[i - 1])
{
src[mostIndex] = src[i - 1];
src[i - 1] = most;
}
}
}
return src;
}
Dictionary<int, List<string>> firstSortedString = new Dictionary<int, List<string>>();
List<string> result = new List<string>();
foreach (string i in source)
{
if (!firstSortedString.ContainsKey(i.Length))
{
firstSortedString.Add(i.Length, new List<string>());
}
}
foreach (string i in source)
{
firstSortedString[i.Length].Add(i);
}
foreach (int i in sortKeys(firstSortedString.Keys.ToArray()))
{
foreach (string j in firstSortedString[i])
{
result.Add(j);
}
}
return result.ToArray();
}
}
|