How List.Sort() indecates where the salaries in the list will be ordered and how to be ordered (asc or desc)?

using System;
using System.Collections.Generic;

class Employee : IComparable<Employee>
{
   public int Salary { get; set; }
   public string Name { get; set; }

   public int CompareTo(Employee other)
   {
      // Alphabetic sort if salary is equal. [A to Z]
      if (this.Salary == other.Salary)
      {
          return this.Name.CompareTo(other.Name);
      }
      // Default to salary sort. [High to low]
      return other.Salary.CompareTo(this.Salary);
   }

   public override string ToString()
   {
      // String representation.
      return this.Salary.ToString() + "," + this.Name;
   }
}

class Program
{
   static void Main()
   {
      List<Employee> list = new List<Employee>();
      list.Add(new Employee() { Name = "Steve", Salary = 10000 });
      list.Add(new Employee() { Name = "Janet", Salary = 10000 });
      list.Add(new Employee() { Name = "Andrew", Salary = 10000 });
      list.Add(new Employee() { Name = "Bill", Salary = 500000 });
      list.Add(new Employee() { Name = "Lucy", Salary = 8000 });

      // Uses IComparable.CompareTo()
      list.Sort();

      // Uses Employee.ToString
      foreach (var element in list)
      {
          Console.WriteLine(element);
      }
   }
}

//OUTPUT
//500000,Bill
//10000,Andrew
//10000,Janet
//10000,Steve
//8000,Lucy

Here's one way to sort ascending or descending:

using System;
using System.Collections.Generic;

class Employee : IComparable<Employee>
{
    public int Salary { get; set; }
    public string Name { get; set; }
    public int CompareTo(Employee other)
    {
        if (Program.SortOrder)
        {
            // Alphabetic sort if salary is equal. [A to Z]
            if (this.Salary == other.Salary)
            {
                return this.Name.CompareTo(other.Name);
            }
            // Default to salary sort. [High to low]
            return other.Salary.CompareTo(this.Salary);
        }
        else
        {
            // Alphabetic sort if salary is equal. [Z to A]
            if (other.Salary == this.Salary)
            {
                return other.Name.CompareTo(this.Name);
            }
            // Default to salary sort. [Low to High]
            return this.Salary.CompareTo(other.Salary); 
        }
    }
    public override string ToString()
    {
        // String representation.
        return this.Salary.ToString() + "," + this.Name;
    }
}
class Program
{
    public static bool SortOrder = true;
    static void Main()
    {

        List<Employee> list = new List<Employee>();
        list.Add(new Employee() { Name = "Steve", Salary = 10000 });
        list.Add(new Employee() { Name = "Janet", Salary = 10000 });
        list.Add(new Employee() { Name = "Andrew", Salary = 10000 });
        list.Add(new Employee() { Name = "Bill", Salary = 500000 });
        list.Add(new Employee() { Name = "Lucy", Salary = 8000 });
        // Uses IComparable.CompareTo() in descending order
        SortOrder = true;
        list.Sort();
        // Uses Employee.ToString
        Console.WriteLine("Descending Order\n\n");
        foreach (var element in list)
        {
            Console.WriteLine(element);
        }
        // Uses IComparable.CompareTo() in ascending order
        SortOrder = false;
        list.Sort();
        // Uses Employee.ToString
        Console.WriteLine("\\n\nAscending Order\n\n");
        foreach (var element in list)
        {
            Console.WriteLine(element);
        }
        Console.ReadLine();
    }
}

I guess you are asking what is the mechanism that says if the sort is to happen ascending or descending. As shown by the code submitted by tinstaafl the ascending or descendsort will occur depending on the value of the variable 'SortOrder' (true or false) that is seen in the 'CompareTo' function.

The two lines of code below are from tinstaafl's program (your original program with the addition of sorting in descending order).

For ascending order:

return this.Name.CompareTo(other.Name);   //Comparing the current object (this) 
                                            with the value that is invoking the 
                                            'CompareTo' function

If the returned integer value of the compare is zero, a positive number, or a negative number, the sort will be ascending.

For descending order:

return other.Name.CompareTo(this.Name);   //Comparing the invoking object (other) 
                                               with the current object (this)

If the returned integer value of the compare is zero, a positive number, or a negative number, the sort will be descending.

I ran (more like 'stepped') your original program through Visual Studio, added duplicate items to the 'list', changed what item invoked 'CompareTo', and could see that the above did work as explained.

It took me a little bit to see how things worked because, like you, I could not readily 'see' what was the mechanism that made things sort in ascending or descending order.

Hope the above helps. tinstaafl's example shows how to implement things both ways.

I will readily admit I am no C# master; so any correction from the gurus here is certainly welcomed.

In searching the web about list sorting, I stumbled upon this masterpiece!

nice catch ddanbe it simplifies the code quite a bit:

using System;
using System.Collections.Generic;

class Employee : IComparable<Employee>
{
    public int Salary { get; set; }
    public string Name { get; set; }
    public int CompareTo(Employee other)
    {

            // Alphabetic sort if salary is equal. [A to Z] or [Z to A]
            if (this.Salary == other.Salary)
            {
                return this.Name.CompareTo(other.Name) * Program.SortOrder;
            }
            // Default to salary sort. [High to Low] or [Low to High]
            return other.Salary.CompareTo(this.Salary) * Program.SortOrder;


    }
    public override string ToString()
    {
        // String representation.
        return this.Salary.ToString() + "," + this.Name;
    }
}
class Program
{
    //change this to -1 to reverse the sort order
    public static int SortOrder =1;
    static void Main()
    {

        List<Employee> list = new List<Employee>();
        list.Add(new Employee() { Name = "Steve", Salary = 10000 });
        list.Add(new Employee() { Name = "Janet", Salary = 10000 });
        list.Add(new Employee() { Name = "Andrew", Salary = 10000 });
        list.Add(new Employee() { Name = "Bill", Salary = 500000 });
        list.Add(new Employee() { Name = "Lucy", Salary = 8000 });
        // Uses IComparable.CompareTo() in descending order
        SortOrder = 1;
        list.Sort();
        // Uses Employee.ToString
        Console.WriteLine("Descending Order\n\n");
        foreach (var element in list)
        {
            Console.WriteLine(element);
        }
        // Uses IComparable.CompareTo() in ascending order
        SortOrder = -1;
        list.Sort();
        // Uses Employee.ToString
        Console.WriteLine("\\n\nAscending Order\n\n");
        foreach (var element in list)
        {
            Console.WriteLine(element);
        }
        Console.ReadLine();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.