本文整理汇总了C#中System.Array.Sort方法的典型用法代码示例。如果您正苦于以下问题:C# Array.Sort方法的具体用法?C# Array.Sort怎么用?C# Array.Sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Array
的用法示例。
在下文中一共展示了Array.Sort方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compare
//引入命名空间
using System;
using System.Collections;
public class ReverseComparer : IComparer
{
// Call CaseInsensitiveComparer.Compare with the parameters reversed.
public int Compare(Object x, Object y)
{
return (new CaseInsensitiveComparer()).Compare(y, x );
}
}
public class Example
{
public static void Main()
{
// Create and initialize a new array.
String[] words = { "The", "QUICK", "BROWN", "FOX", "jumps",
"over", "the", "lazy", "dog" };
// Instantiate the reverse comparer.
IComparer revComparer = new ReverseComparer();
// Display the values of the array.
Console.WriteLine( "The original order of elements in the array:" );
DisplayValues(words);
// Sort a section of the array using the default comparer.
Array.Sort(words, 1, 3);
Console.WriteLine( "After sorting elements 1-3 by using the default comparer:");
DisplayValues(words);
// Sort a section of the array using the reverse case-insensitive comparer.
Array.Sort(words, 1, 3, revComparer);
Console.WriteLine( "After sorting elements 1-3 by using the reverse case-insensitive comparer:");
DisplayValues(words);
// Sort the entire array using the default comparer.
Array.Sort(words);
Console.WriteLine( "After sorting the entire array by using the default comparer:");
DisplayValues(words);
// Sort the entire array by using the reverse case-insensitive comparer.
Array.Sort(words, revComparer);
Console.WriteLine( "After sorting the entire array using the reverse case-insensitive comparer:");
DisplayValues(words);
}
public static void DisplayValues(String[] arr)
{
for ( int i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0);
i++ ) {
Console.WriteLine( " [{0}] : {1}", i, arr[i] );
}
Console.WriteLine();
}
}
输出:
The original order of elements in the array: [0] : The [1] : QUICK [2] : BROWN [3] : FOX [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog After sorting elements 1-3 by using the default comparer: [0] : The [1] : BROWN [2] : FOX [3] : QUICK [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog After sorting elements 1-3 by using the reverse case-insensitive comparer: [0] : The [1] : QUICK [2] : FOX [3] : BROWN [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog After sorting the entire array by using the default comparer: [0] : BROWN [1] : dog [2] : FOX [3] : jumps [4] : lazy [5] : over [6] : QUICK [7] : the [8] : The After sorting the entire array using the reverse case-insensitive comparer: [0] : the [1] : The [2] : QUICK [3] : over [4] : lazy [5] : jumps [6] : FOX [7] : dog [8] : BROWN
示例2: return
//引入命名空间
using System;
using System.Collections;
public class SamplesArray {
public class myReverserClass : IComparer {
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}
}
public static void Main() {
// Creates and initializes a new Array and a new custom comparer.
String[] myKeys = { "red", "GREEN", "YELLOW", "BLUE", "purple", "black", "orange" };
String[] myValues = { "strawberries", "PEARS", "LIMES", "BERRIES", "grapes", "olives", "cantaloupe" };
IComparer myComparer = new myReverserClass();
// Displays the values of the Array.
Console.WriteLine( "The Array initially contains the following values:" );
PrintKeysAndValues( myKeys, myValues );
// Sorts a section of the Array using the default comparer.
Array.Sort( myKeys, myValues, 1, 3 );
Console.WriteLine( "After sorting a section of the Array using the default comparer:" );
PrintKeysAndValues( myKeys, myValues );
// Sorts a section of the Array using the reverse case-insensitive comparer.
Array.Sort( myKeys, myValues, 1, 3, myComparer );
Console.WriteLine( "After sorting a section of the Array using the reverse case-insensitive comparer:" );
PrintKeysAndValues( myKeys, myValues );
// Sorts the entire Array using the default comparer.
Array.Sort( myKeys, myValues );
Console.WriteLine( "After sorting the entire Array using the default comparer:" );
PrintKeysAndValues( myKeys, myValues );
// Sorts the entire Array using the reverse case-insensitive comparer.
Array.Sort( myKeys, myValues, myComparer );
Console.WriteLine( "After sorting the entire Array using the reverse case-insensitive comparer:" );
PrintKeysAndValues( myKeys, myValues );
}
public static void PrintKeysAndValues( String[] myKeys, String[] myValues ) {
for ( int i = 0; i < myKeys.Length; i++ ) {
Console.WriteLine( " {0,-10}: {1}", myKeys[i], myValues[i] );
}
Console.WriteLine();
}
}
输出:
The Array initially contains the following values: red : strawberries GREEN : PEARS YELLOW : LIMES BLUE : BERRIES purple : grapes black : olives orange : cantaloupe After sorting a section of the Array using the default comparer: red : strawberries BLUE : BERRIES GREEN : PEARS YELLOW : LIMES purple : grapes black : olives orange : cantaloupe After sorting a section of the Array using the reverse case-insensitive comparer: red : strawberries YELLOW : LIMES GREEN : PEARS BLUE : BERRIES purple : grapes black : olives orange : cantaloupe After sorting the entire Array using the default comparer: black : olives BLUE : BERRIES GREEN : PEARS orange : cantaloupe purple : grapes red : strawberries YELLOW : LIMES After sorting the entire Array using the reverse case-insensitive comparer: YELLOW : LIMES red : strawberries purple : grapes orange : cantaloupe GREEN : PEARS BLUE : BERRIES black : olives
示例3: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
string[] dinosaurs = {"Pachycephalosaurus",
"Amargasaurus",
"Tyrannosaurus",
"Mamenchisaurus",
"Deinonychus",
"Edmontosaurus"};
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nSort");
Array.Sort(dinosaurs);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch for 'Coelophysis':");
int index = Array.BinarySearch(dinosaurs, "Coelophysis");
ShowWhere(dinosaurs, index);
Console.WriteLine("\nBinarySearch for 'Tyrannosaurus':");
index = Array.BinarySearch(dinosaurs, "Tyrannosaurus");
ShowWhere(dinosaurs, index);
}
private static void ShowWhere<T>(T[] array, int index)
{
if (index<0)
{
// If the index is negative, it represents the bitwise
// complement of the next larger element in the array.
//
index = ~index;
Console.Write("Not found. Sorts between: ");
if (index == 0)
Console.Write("beginning of array and ");
else
Console.Write("{0} and ", array[index-1]);
if (index == array.Length)
Console.WriteLine("end of array.");
else
Console.WriteLine("{0}.", array[index]);
}
else
{
Console.WriteLine("Found at index {0}.", index);
}
}
}
输出:
Pachycephalosaurus Amargasaurus Tyrannosaurus Mamenchisaurus Deinonychus Edmontosaurus Sort Amargasaurus Deinonychus Edmontosaurus Mamenchisaurus Pachycephalosaurus Tyrannosaurus BinarySearch for 'Coelophysis': Not found. Sorts between: Amargasaurus and Deinonychus. BinarySearch for 'Tyrannosaurus': Found at index 5.
示例4: Compare
//引入命名空间
using System;
using System.Collections.Generic;
public class ReverseComparer: IComparer<string>
{
public int Compare(string x, string y)
{
// Compare y and x in reverse order.
return y.CompareTo(x);
}
}
public class Example
{
public static void Main()
{
string[] dinosaurs = {"Pachycephalosaurus",
"Amargasaurus",
"Tyrannosaurus",
"Mamenchisaurus",
"Deinonychus",
"Edmontosaurus"};
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
ReverseComparer rc = new ReverseComparer();
Console.WriteLine("\nSort");
Array.Sort(dinosaurs, rc);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch for 'Coelophysis':");
int index = Array.BinarySearch(dinosaurs, "Coelophysis", rc);
ShowWhere(dinosaurs, index);
Console.WriteLine("\nBinarySearch for 'Tyrannosaurus':");
index = Array.BinarySearch(dinosaurs, "Tyrannosaurus", rc);
ShowWhere(dinosaurs, index);
}
private static void ShowWhere<T>(T[] array, int index)
{
if (index<0)
{
// If the index is negative, it represents the bitwise
// complement of the next larger element in the array.
//
index = ~index;
Console.Write("Not found. Sorts between: ");
if (index == 0)
Console.Write("beginning of array and ");
else
Console.Write("{0} and ", array[index-1]);
if (index == array.Length)
Console.WriteLine("end of array.");
else
Console.WriteLine("{0}.", array[index]);
}
else
{
Console.WriteLine("Found at index {0}.", index);
}
}
}
输出:
Pachycephalosaurus Amargasaurus Tyrannosaurus Mamenchisaurus Deinonychus Edmontosaurus Sort Tyrannosaurus Pachycephalosaurus Mamenchisaurus Edmontosaurus Deinonychus Amargasaurus BinarySearch for 'Coelophysis': Not found. Sorts between: Deinonychus and Amargasaurus. BinarySearch for 'Tyrannosaurus': Found at index 0.
示例5: CompareDinosByLength
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
private static int CompareDinosByLength(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length);
if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
}
public static void Main()
{
string[] dinosaurs = {
"Pachycephalosaurus",
"Amargasaurus",
"",
null,
"Mamenchisaurus",
"Deinonychus" };
Display(dinosaurs);
Console.WriteLine("\nSort with generic Comparison<string> delegate:");
Array.Sort(dinosaurs, CompareDinosByLength);
Display(dinosaurs);
}
private static void Display(string[] arr)
{
Console.WriteLine();
foreach( string s in arr )
{
if (s == null)
Console.WriteLine("(null)");
else
Console.WriteLine("\"{0}\"", s);
}
}
}
输出:
"Pachycephalosaurus" "Amargasaurus" "" (null) "Mamenchisaurus" "Deinonychus" Sort with generic Comparisondelegate: (null) "" "Deinonychus" "Amargasaurus" "Mamenchisaurus" "Pachycephalosaurus"
示例6: Compare
//引入命名空间
using System;
using System.Collections.Generic;
public class ReverseComparer: IComparer<string>
{
public int Compare(string x, string y)
{
// Compare y and x in reverse order.
return y.CompareTo(x);
}
}
public class Example
{
public static void Main()
{
string[] dinosaurs = {"Pachycephalosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Tarbosaurus",
"Tyrannosaurus",
"Albertasaurus"};
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nSort(dinosaurs, 3, 3)");
Array.Sort(dinosaurs, 3, 3);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
ReverseComparer rc = new ReverseComparer();
Console.WriteLine("\nSort(dinosaurs, 3, 3, rc)");
Array.Sort(dinosaurs, 3, 3, rc);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
}
}
输出:
Pachycephalosaurus Amargasaurus Mamenchisaurus Tarbosaurus Tyrannosaurus Albertasaurus Sort(dinosaurs, 3, 3) Pachycephalosaurus Amargasaurus Mamenchisaurus Albertasaurus Tarbosaurus Tyrannosaurus Sort(dinosaurs, 3, 3, rc) Pachycephalosaurus Amargasaurus Mamenchisaurus Tyrannosaurus Tarbosaurus Albertasaurus
示例7: Compare
//引入命名空间
using System;
using System.Collections.Generic;
public class ReverseComparer: IComparer<string>
{
public int Compare(string x, string y)
{
// Compare y and x in reverse order.
return y.CompareTo(x);
}
}
public class Example
{
public static void Main()
{
string[] dinosaurs = {
"Seismosaurus",
"Chasmosaurus",
"Coelophysis",
"Mamenchisaurus",
"Caudipteryx",
"Cetiosaurus" };
int[] dinosaurSizes = { 40, 5, 3, 22, 1, 18 };
Console.WriteLine();
for (int i = 0; i < dinosaurs.Length; i++)
{
Console.WriteLine("{0}: up to {1} meters long.",
dinosaurs[i], dinosaurSizes[i]);
}
Console.WriteLine("\nSort(dinosaurs, dinosaurSizes)");
Array.Sort(dinosaurs, dinosaurSizes);
Console.WriteLine();
for (int i = 0; i < dinosaurs.Length; i++)
{
Console.WriteLine("{0}: up to {1} meters long.",
dinosaurs[i], dinosaurSizes[i]);
}
ReverseComparer rc = new ReverseComparer();
Console.WriteLine("\nSort(dinosaurs, dinosaurSizes, rc)");
Array.Sort(dinosaurs, dinosaurSizes, rc);
Console.WriteLine();
for (int i = 0; i < dinosaurs.Length; i++)
{
Console.WriteLine("{0}: up to {1} meters long.",
dinosaurs[i], dinosaurSizes[i]);
}
Console.WriteLine("\nSort(dinosaurs, dinosaurSizes, 3, 3)");
Array.Sort(dinosaurs, dinosaurSizes, 3, 3);
Console.WriteLine();
for (int i = 0; i < dinosaurs.Length; i++)
{
Console.WriteLine("{0}: up to {1} meters long.",
dinosaurs[i], dinosaurSizes[i]);
}
Console.WriteLine("\nSort(dinosaurs, dinosaurSizes, 3, 3, rc)");
Array.Sort(dinosaurs, dinosaurSizes, 3, 3, rc);
Console.WriteLine();
for (int i = 0; i < dinosaurs.Length; i++)
{
Console.WriteLine("{0}: up to {1} meters long.",
dinosaurs[i], dinosaurSizes[i]);
}
}
}
输出:
Seismosaurus: up to 40 meters long. Chasmosaurus: up to 5 meters long. Coelophysis: up to 3 meters long. Mamenchisaurus: up to 22 meters long. Caudipteryx: up to 1 meters long. Cetiosaurus: up to 18 meters long. Sort(dinosaurs, dinosaurSizes) Caudipteryx: up to 1 meters long. Cetiosaurus: up to 18 meters long. Chasmosaurus: up to 5 meters long. Coelophysis: up to 3 meters long. Mamenchisaurus: up to 22 meters long. Seismosaurus: up to 40 meters long. Sort(dinosaurs, dinosaurSizes, rc) Seismosaurus: up to 40 meters long. Mamenchisaurus: up to 22 meters long. Coelophysis: up to 3 meters long. Chasmosaurus: up to 5 meters long. Cetiosaurus: up to 18 meters long. Caudipteryx: up to 1 meters long. Sort(dinosaurs, dinosaurSizes, 3, 3) Seismosaurus: up to 40 meters long. Mamenchisaurus: up to 22 meters long. Coelophysis: up to 3 meters long. Caudipteryx: up to 1 meters long. Cetiosaurus: up to 18 meters long. Chasmosaurus: up to 5 meters long. Sort(dinosaurs, dinosaurSizes, 3, 3, rc) Seismosaurus: up to 40 meters long. Mamenchisaurus: up to 22 meters long. Coelophysis: up to 3 meters long. Chasmosaurus: up to 5 meters long. Cetiosaurus: up to 18 meters long. Caudipteryx: up to 1 meters long.
示例8: Main
//引入命名空间
using System;
class MainClass
{
public static void Main()
{
char[] charArray = {'w', 'e', 'l', 'c', 'o', 'm', 'e'};
Array.Sort(charArray); // sort the elements
Console.WriteLine("Sorted charArray:");
for (int i = 0; i < charArray.Length; i++)
{
Console.WriteLine("charArray[" + i + "] = " +
charArray[i]);
}
}
}
示例9: Array.Sort(names, Comparer.DefaultInvariant)
//引入命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Globalization;
class Program {
static void DisplayNames(IEnumerable e) {
foreach (string s in e)
Console.Write(s + " - ");
}
static void Main(string[] args) {
string[] names = {"Alabama", "Texas", "Washington",
"Virginia", "Wisconsin", "Wyoming",
"Kentucky", "Missouri", "Utah", "Hawaii",
"Kansas", "Lousiana", "Alaska", "Arizona"};
Thread.CurrentThread.CurrentCulture = new CultureInfo("fi-FI");
Array.Sort(names);
DisplayNames(names);
Array.Sort(names, Comparer.DefaultInvariant);
Console.WriteLine("\nsorted with invariant culture...");
DisplayNames(names);
}
}