SoFunction
Updated on 2025-03-01

C# implementation uses subtotal

1. Use () to sort the basic numerical type data

Case: Sort the List where int data is stored

In fact, the comparison function CompareTo in List Sort function in C# has three results: 1, -1, and 0 represent large, small, and equal respectively. The default order of List is sorted ascending.

For example: In the comparison function CompareTo(), if x>y return 1; is arranged in ascending order. If x>y return -1; is arranged in descending order. This is what size 1 and -1 means. In fact, you have to write x<y return 1; it also means descending order. However, everyone is generally used to x>y return 1; ascending order. If you want to descend, just return -1;

Tips: The default sorting of the system List is ascending order. If you want to descend, you can directly add a negative sign in front of the comparison function and change the return result from 1 to -1. For example:

List<int> list = new List<int>() { 2, 1, 3, 4, 5 };
((x, y) => -(y));	
for (int i = 0; i < (); i++)
{
    (list[i] + " ");
}
();

2. Use () to sort non-numeric types, strings, structures, objects, etc. or types that have not officially implemented the IComparable interface.

Case:
Suppose we currently have a Person class and a List that stores Person class data, as follows

public class Person
{
    public int id;
    public string name;

    public Person()
    {
        id = 0;
        name = "name";
    }
    public Person(int id_, string name_)
    {
        id = id_;
        name = name_;
    }
 }


static void Main(string[] args)
{
    List<Person> list2 = new List<Person>();
    (new Person(10001, "Tom"));
    (new Person(10003, "Jerry"));
    (new Person(10002, "Ben"));
    (new Person(10005, "Cat"));
    (new Person(10004, "Danny"));
}

Suppose our current requirement is to sort this from small to large by id.
We have two ways:

Method 1: Implement IComparable interface rewrite CompareTo method

Code:

public class Person : IComparable&lt;Person&gt;
{
    public int id;
    public string name;

    public Person()
    {
        id = 0;
        name = "name";
    }
    public Person(int id_, string name_)
    {
        id = id_;
        name = name_;
    }

    public int CompareTo(Person obj_)
    {
        if ( &gt; obj_.id)
            return 1;
        else
            return -1;
    }
}

static void Main(string[] args)
{
    List&lt;int&gt; list = new List&lt;int&gt;() { 2, 1, 3, 4, 5 };
    ((x, y) =&gt; -(y));
    for (int i = 0; i &lt; (); i++)
    {
        (list[i] + " ");
    }
    ();
    ();

    List&lt;Person&gt; list2 = new List&lt;Person&gt;();
    (new Person(10001, "Tom"));
    (new Person(10003, "Jerry"));
    (new Person(10002, "Ben"));
    (new Person(10005, "Cat"));
    (new Person(10004, "Danny"));
    // The first way to customize the sorting rules of object structures: implement the IComparable interface in the class    ();
    for (int i = 0; i &lt; (); i++)
    {
        (list2[i].id + " " + list2[i].name);
    }
    ();
}

Method 2: Write a sorting function and pass it into it when calling().

Code:

public class Person
{
     public int id;
     public string name;

     public Person()
     {
         id = 0;
         name = "name";
     }
     public Person(int id_, string name_)
     {
         id = id_;
         name = name_;
     }
}

private static int CmpFun(Person a, Person b)
{
    if ( &gt; )
        return 1;
    else
        return -1;
}


static void Main(string[] args)
{
    List&lt;Person&gt; list2 = new List&lt;Person&gt;();
    (new Person(10001, "Tom"));
    (new Person(10003, "Jerry"));
    (new Person(10002, "Ben"));
    (new Person(10005, "Cat"));
    (new Person(10004, "Danny"));
	
	// The second way to customize the sorting rules of object structures: write a sorting rule function and pass it into    (CmpFun);
    for (int i = 0; i &lt; (); i++)
    {
        (list2[i].id + " " + list2[i].name);
    }
    ();
}

Note that the method name is used without brackets. Using a method as a parameter will inform the compiler that converts a method reference to a reference that can be used as a delegate call target, and attaches the method as the call target.

This is the end of this article about C# implementation () subtotal use. For more related C# () content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!