SoFunction
Updated on 2025-03-06

Two common methods of sorting C# arrays

This article describes two common methods of sorting C# arrays. Share it for your reference. The details are as follows:

1. The first example

Define the code

#region Array Sort 1public class Pigeon : IComparable<Pigeon>
//The class element itself inherits the comparison interface{
int XValue;
int YValue;
public string BatchNo { get; set; }
public int CompareTo(Pigeon other)
{
  if (other == null)
 throw new ArgumentNullException("Compare parameters are empty");
  //Compare by the third character  XValue = Convert.ToInt32((2, 1));
  YValue = Convert.ToInt32((2, 1));
  if (XValue == YValue)
  {
 return 0;
  }
  else if (XValue < YValue)
  {
 return -1;   //Return -1, X is ahead of Y  }
  else
  {
 return 1;    //Return 1, X is behind Y  }
}
}
#endregion

Test code

#region Test Array Array Sort 1private void button3_Click(object sender, EventArgs e)
{
  Pigeon[] pigeons = new Pigeon[]{
 new Pigeon(){BatchNo="1256"},
 new Pigeon(){BatchNo="1236"},
 new Pigeon(){BatchNo="1276"},
 new Pigeon(){BatchNo="1216"}
  };
  (pigeons);
  string ResultMsg = "";
  foreach (Pigeon o in pigeons)
  {
 ResultMsg +=  + "\r\n";
  }
  (ResultMsg);
}
#endregion

1. The second example

Define the code

#region Array Orderpublic class MyBatch   //Array element class{
public string BatchNo { get; set; }
}
public enum CompareType   //Compare type enumeration{
ThreeChar =0,
FourChar=1
}
public class MyBatchCompare:IComparer<MyBatch>
//Define a comparison class{
private CompareType compareType;//Define the sort typepublic MyBatchCompare(CompareType compareType)
{
  //Initialize comparison type in constructor   = compareType;
}
public int Compare(MyBatch x, MyBatch y)
{
  int XValue;
  int YValue;
  if (x == null) throw new ArgumentNullException("The value of x is empty");
  if (y == null) throw new ArgumentNullException("Y value is empty");
  switch (compareType)
  {          
 case :
   //Sorting from small to large by the third character   XValue = Convert.ToInt32((2,1));
   YValue = Convert.ToInt32((2,1));
   if (XValue == YValue)
   {
 return 0;
   }
   else if (XValue < YValue)
   {
 return -1;   //Return -1, X is ahead of Y   }
   else
   {
 return 1;    //Return 1, X is behind Y   }
 case :
   //Sorting from small to large by the fourth character   XValue = Convert.ToInt32((3,1));
   YValue = Convert.ToInt32((3,1));
   if (XValue == YValue)
   {
 return 0;
   }
   else if (XValue < YValue)
   {
 return -1;   //Return -1, X is ahead of Y   }
   else
   {
 return 1;    //Return 1, X is behind Y   }
 default:
   throw new ArgumentException("Compare Type Parameter Error");
  }
}
}
#endregion

Test code

#region Array Orderprivate void button2_Click(object sender, EventArgs e)
{
  MyBatch[] batchs ={
   new MyBatch(){BatchNo="1234"},
   new MyBatch(){BatchNo="1263"},
   new MyBatch(){BatchNo="1218"},
   new MyBatch(){BatchNo="1242"}
    };
  //Sort from small to large according to the third character  (batchs,new MyBatchCompare());
  string ResultMsg = "";
  foreach (MyBatch o in batchs)
  {
 ResultMsg +=  + "\r\n";
  }
  (ResultMsg);
  //Sort from small to large by the fourth character  (batchs, new MyBatchCompare());
  ResultMsg = "";
  foreach (MyBatch o in batchs)
  {
 ResultMsg +=  + "\r\n";
  }
  (ResultMsg);
}
#endregion

I hope this article will be helpful to everyone's C# programming.