C#Sort排序

List 的Sort方法排序有三种结果 1,0,-1分别表示大于,等于,小于。 
 

1.对于数值类型的List (List<int>),直接使用Sort进行排序。

[csharp]  view plain  copy
  1. List<int> scoreList=new List<int>(){89,100,78,23,67};  
  2.   
  3. scoreList.Sort();//默认按升序排列,相当于:scoreList.Sort((x,y)=>x.CompareTo(y))  
  4.   
  5. scoreList.Sort((x,y)=>-x.CompareTo(y));//降序排列  

2.对于非数值类型或者自定义类型,可通过实现IComparable接口重写CompareTo方法来排序:

[csharp]  view plain  copy
  1. public class Person : IComparable<Person>  
  2.     {  
  3.         public string Name { getset; }  
  4.         public int Age { getset; }  
  5.   
  6.         //ComparetTo:大于 1; 等于 0; 小于 -1;  
  7.         public int CompareTo(Person p)  
  8.         {  
  9.             int result;  
  10.             if (this.Name == p.Name && this.Age == p.Age)  
  11.             {  
  12.                 result = 0;  
  13.             }  
  14.             else  
  15.             {  
  16.                 //this.Name表示后面的 Mary p.Name表示前面的 Bob  
  17.                 //Mary 跟Bob 由小到大比较,如果Mary 与 Bob 比较 大于0(说明Mary 大于Bob),则 result=1(说明是由小到大的顺序)  
  18.                 if (this.Name.CompareTo(p.Name) > 0)//先按名字小到大排列  
  19.                 {  
  20.                     result = 1;  
  21.                 }  
  22.                 else if (this.Name == p.Name && this.Age > p.Age)//名字相同则按年龄由小到大排列  
  23.                 {  
  24.                     result = 1;  
  25.                 }  
  26.                 else  
  27.                 {  
  28.                     result = -1;  
  29.                 }  
  30.             }  
  31.             return result;  
  32.         }  
  33.   
  34.         public override string ToString()  
  35.         {  
  36.             return this.Name + "-" + this.Age;  
  37.         }  
  38.     }  
[csharp]  view plain  copy
  1. List<Person> lstPerson = new List<Person>();  
  2.   lstPerson.Add(new Person() { Name = "Bob", Age = 19 });  
  3.   lstPerson.Add(new Person() { Name = "Mary", Age = 18 });  
  4.   lstPerson.Add(new Person() { Name = "Mary", Age = 17 });  
  5.   lstPerson.Add(new Person() { Name = "Lily", Age = 20 });  
  6.   lstPerson.Sort();  
  7.   foreach (Person item in lstPerson)  
  8.   {  
  9.       Console.WriteLine(item.ToString());  
  10.   }  
  11.   Console.ReadKey();  

输出:Bob-19 Lily-20 Mary-17 Mary-18 
或不实现IComparable接口而使用linq排序:

[csharp]  view plain  copy
  1. List<Person> lstPerson = new List<Person>();  
  2.           lstPerson.Add(new Person() { Name = "Bob", Age = 19 });  
  3.           lstPerson.Add(new Person() { Name = "Mary", Age = 18 });  
  4.           lstPerson.Add(new Person() { Name = "Mary", Age = 17 });  
  5.           lstPerson.Add(new Person() { Name = "Lily", Age = 20 });  
  6.           lstPerson.Sort();  
  7.   
  8.           lstPerson.Sort((x, y) => {  
  9.               int result;  
  10.               if (x.Name == y.Name && x.Age == y.Age)  
  11.               {  
  12.                   result = 0;  
  13.               }  
  14.               else  
  15.               {  
  16.                   if (x.Name.CompareTo(y.Name) > 0)  
  17.                   {  
  18.                       result = 1;  
  19.                   }  
  20.                   else if (x.Name == y.Name && x.Age > y.Age)  
  21.                   {  
  22.                       result = 1;  
  23.                   }  
  24.                   else  
  25.                   {  
  26.                       result = -1;  
  27.                   }  
  28.               }  
  29.               return result;  
  30.           });  
  31.   
  32.   
  33.           foreach (Person item in lstPerson)  
  34.           {  
  35.               Console.WriteLine(item.ToString());  
  36.           }  
  37.           Console.ReadKey();  

输出:Bob-19 Lily-20 Mary-17 Mary-18

  public int sortOrderForCharacter(AccessoriesProfile a, AccessoriesProfile b)
    {
        bool isACan = IsCanSelectForFit(a);//是否是能够选中的配件
        bool isBCan = IsCanSelectForFit(b);//是否是能够选中的配件

        if (a == b)
            return 0;
        else
        {
            if (a.AccessorStar == b.AccessorStar && isACan == isBCan && a.InstanceID == b.InstanceID)
                return 0;    
            else
            {
                if (isACan == isBCan)
                {
                    if (a.AccessorStar == b.AccessorStar)
                    {
                        if (a.InstanceID == b.InstanceID)
                        {
                            return 0;
                        }
                        else
                        {
                            if (a.InstanceID > b.InstanceID)
                                return -1;
                            else
                                return 1;
                        }
                    }
                    else
                    {
                        if (a.AccessorStar > b.AccessorStar)
                            return -1;
                        else
                            return 1;
                    }
                }
                else
                {
                    if (isACan && !isBCan)
                        return -1;
                    else
                        return 1;
                }
            }
	}
   }

排序规则:优先级越高的越先对其进行排序,比如上面的代码,就是先对能不能被选中先排序,其次是星级,和id



猜你喜欢

转载自blog.csdn.net/sun1018974080/article/details/78094978