Csharp/CSharp Tutorial/Data Structure/Array Sort

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Display the contents of the sorted array

<source lang="csharp">using System; using System.Collections; class MainClass {

   public static void Main(string[] args)
   {
           // Create a new array and populate it.
           int[] array = { 4, 2, 9, 3 };
           // Sort the array.
           Array.Sort(array);
           foreach (int i in array) { 
               Console.WriteLine(i); 
           }
   
   }

}</source>

2
3
4
9

Implementing IComparable and sort by Array.Sort

<source lang="csharp">using System; public class Employee: IComparable{

   public Employee(string name, int id)
   {
       this.name = name;
       this.id = id;
   }
   
   int IComparable.rupareTo(object obj)
   {
       Employee emp2 = (Employee) obj;
       if (this.id > emp2.id)
           return(1);
       if (this.id < emp2.id)
           return(-1);
       else
           return(0);
   }
   
   public override string ToString()
   {
       return(String.Format("{0}:{1}", name, id));
   }
   
   string name;
   int id;

} class MainClass {

   public static void Main()
   {
       Employee[] arr = new Employee[4];
       arr[0] = new Employee("A", 1);
       arr[1] = new Employee("B", 2);
       arr[2] = new Employee("C", 4);
       arr[3] = new Employee("D", 3);
       
       Array.Sort(arr);
       foreach (Employee emp in arr)
           Console.WriteLine("Employee: {0}", emp);
       
       Console.WriteLine("Find employee id 2 in the list");
       
       Employee employeeToFind = new Employee(null, 2);
       int index = Array.BinarySearch(arr, employeeToFind);
       if (index != -1)
           Console.WriteLine("Found: {0}", arr[index]);    
   }

}</source>

Employee: A:1
Employee: B:2
Employee: D:3
Employee: C:4
Find employee id 2 in the list
Found: B:2

Sort an array

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   int[] nums = { 5, 4, 6, 3, 14, 9, 8, 17, 1, 24, -1, 0 }; 
  
   Console.Write("Original order: "); 
   foreach(int i in nums)  
     Console.Write(i + " "); 
   Console.WriteLine(); 

   Array.Sort(nums); 

   Console.Write("Sorted order:   "); 
   foreach(int i in nums)  
     Console.Write(i + " "); 
   Console.WriteLine(); 


 }     

}</source>

Original order: 5 4 6 3 14 9 8 17 1 24 -1 0
Sorted order:   -1 0 1 3 4 5 6 8 9 14 17 24

Sort an array and search for a value

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   int[] nums = { 5, 4, 6, 3, 14, 9, 8, 17, 1, 24, -1, 0 }; 
  
   Array.Sort(nums); 

    // Search for 14. 
   int idx = Array.BinarySearch(nums, 14); 

   Console.WriteLine("Index of 14 is " + idx); 
 }     

}</source>

Index of 14 is 9

Use Array.Sort to sort object array

<source lang="csharp">using System; using System.Collections.Generic; using System.Text; class MyClass : IComparable {

  public int TheValue;
  public int CompareTo(object obj)            
  {
     MyClass mc = (MyClass)obj;
     
     if (this.TheValue < mc.TheValue) 
        return -1;
     
     if (this.TheValue > mc.TheValue) 
        return 1;
     
     return 0;
  }

} class MainClass {

  static void Main()
  {
     MyClass[] objectArray = new MyClass[5];         
     for (int i = 0; i < 5; i++)              
     {
        objectArray[i] = new MyClass();
        objectArray[i].TheValue = 100 - i;
     }
     foreach (MyClass i in objectArray)
        Console.Write("{0} ", i.TheValue);
     Array.Sort(objectArray);                        
     foreach (MyClass i in objectArray)
        Console.Write("{0} ", i.TheValue);
  }

}</source>

100 99 98 97 96 96 97 98 99 100

Use System.Array.Sort() to sort an int array

<source lang="csharp">using System; class MainClass {

   public static void Main()
   {
       int[] arr = {5, 1, 10, 33, 100, 4};
       Array.Sort(arr);
       foreach (int v in arr)
          Console.WriteLine("Element: {0}", v);
   }

}</source>

Element: 1
Element: 4
Element: 5
Element: 10
Element: 33
Element: 100

Use the Sort() method to sort the elements in an int array

<source lang="csharp">using System; class MainClass {

 public static void Main()
 {
   int[] intArray = {5, 2, 3, 1, 6, 9, 7, 14, 25};
   Array.Sort(intArray);
   Console.WriteLine("Sorted intArray:");
   for (int i = 0; i < intArray.Length; i++)
   {
     Console.WriteLine("intArray[" + i + "] = " +
       intArray[i]);
   }
 }

}</source>

Sorted intArray:
intArray[0] = 1
intArray[1] = 2
intArray[2] = 3
intArray[3] = 5
intArray[4] = 6
intArray[5] = 7
intArray[6] = 9
intArray[7] = 14
intArray[8] = 25