在c# 中怎样用reverse和sort来实现数组的降序排列?
在c#中学到reverse(反转)方法时说到“将该方法与sort方法结合,可以实现数组的降序排序”,但我还是不会用,希望有人能解释一下,最好再举个例子!谢了!...
在c#中学到reverse(反转)方法时说到“将该方法与sort方法结合,可以实现数组的降序排序”,但我还是不会用,希望有人能解释一下,最好再举个例子!谢了!
展开
1个回答
展开全部
网上抄的:
在 .NET Framework 中,数组是作为 Array 类的实例实现的。此类提供了许多有用的方法,如 Sort 和 Reverse。
下面的示例演示了使用这些方法是多么的简单。首先,使用 Reverse 方法将数组元素反转,然后使用 Sort 方法对它们进行排序:
C#
class ArrayMethods
{
static void Main()
{
// Create a string array of size 5:
string[] employeeNames = new string[5];
// Read 5 employee names from user:
System.Console.WriteLine("Enter five employee names:");
for(int i=0; i<employeeNames.Length; i++)
{
employeeNames[i]= System.Console.ReadLine();
}
// Print the array in original order:
System.Console.WriteLine("\nArray in Original Order:");
foreach(string employeeName in employeeNames)
{
System.Console.Write("{0} ", employeeName);
}
// Reverse the array:
System.Array.Reverse(employeeNames);
// Print the array in reverse order:
System.Console.WriteLine("\n\nArray in Reverse Order:");
foreach(string employeeName in employeeNames)
{
System.Console.Write("{0} ", employeeName);
}
// Sort the array:
System.Array.Sort(employeeNames);
// Print the array in sorted order:
System.Console.WriteLine("\n\nArray in Sorted Order:");
foreach(string employeeName in employeeNames)
{
System.Console.Write("{0} ", employeeName);
}
}
}
输出
Enter five employee names:
Luca
Angie
Brian
Kent
Beatriz
Array in Original Order:
Luca Angie Brian Kent Beatriz
Array in Reverse Order:
Beatriz Kent Brian Angie Luca
Array in Sorted Order:
Angie Beatriz Brian Kent Luca
在 .NET Framework 中,数组是作为 Array 类的实例实现的。此类提供了许多有用的方法,如 Sort 和 Reverse。
下面的示例演示了使用这些方法是多么的简单。首先,使用 Reverse 方法将数组元素反转,然后使用 Sort 方法对它们进行排序:
C#
class ArrayMethods
{
static void Main()
{
// Create a string array of size 5:
string[] employeeNames = new string[5];
// Read 5 employee names from user:
System.Console.WriteLine("Enter five employee names:");
for(int i=0; i<employeeNames.Length; i++)
{
employeeNames[i]= System.Console.ReadLine();
}
// Print the array in original order:
System.Console.WriteLine("\nArray in Original Order:");
foreach(string employeeName in employeeNames)
{
System.Console.Write("{0} ", employeeName);
}
// Reverse the array:
System.Array.Reverse(employeeNames);
// Print the array in reverse order:
System.Console.WriteLine("\n\nArray in Reverse Order:");
foreach(string employeeName in employeeNames)
{
System.Console.Write("{0} ", employeeName);
}
// Sort the array:
System.Array.Sort(employeeNames);
// Print the array in sorted order:
System.Console.WriteLine("\n\nArray in Sorted Order:");
foreach(string employeeName in employeeNames)
{
System.Console.Write("{0} ", employeeName);
}
}
}
输出
Enter five employee names:
Luca
Angie
Brian
Kent
Beatriz
Array in Original Order:
Luca Angie Brian Kent Beatriz
Array in Reverse Order:
Beatriz Kent Brian Angie Luca
Array in Sorted Order:
Angie Beatriz Brian Kent Luca
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询