写一个对象集合排序的类

 

要让返回数据集合MyList<T>有排序的功能,首先要让MyList<user>继承List<T>,然后调用MyList对象的sort()排序方法

sort()方法共有四种重载分别是:

1.sort();

2.sort(Comparison<T>comparison)

3.sort(Icomparer(T)comparer)

4.sort(int index,int count,Icomparer<T>Comparer)

 

方法一 sort();

此方法使用类型 T 的默认比较器 Comparer<T>.Default 确定列表元素的顺序。Comparer<T>.Default 属性检查类型 T 是否实现了 IComparable<T>泛型接口,如果实现了该接口,则使用该实现。 否则,Comparer<T>.Default 将检查类型 T 是否实现了 IComparable 接口。 如果类型 T 未实现任一接口,则 Comparer<T>.Default 将引发 InvalidOperationException。

 

方法二 sort(Comparison<T>comparison)

3.sort(Icomparer(T)comparer)

如果提供了 comparison,则使用该委托表示的方法对 List<T>的元素进行排序。

如果 comparisonnullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing,则引发 ArgumentNullException。

comparisond是自己定义的一个排序方法只需将此方法当做采参数传进sort()中即可

此方法的签名为:int comparisond(string x, string y) 方法名是可以自己定义的

下面的代码示例演示 Sort(Comparison<T>) 方法的使用
using System;
using System.Collections.Generic;

public class Example
{
    private static int CompareDinosByLength(string x, string y)
    {
        if (x == null)
        {
            if (y == null)
            {
                              return 0;
            }
            else
            {
                                return -1;
            }
        }
        else
        {
              if (y == null)
                  {
                    return 1;
                 }
            else
            {
                      int retval = x.Length.CompareTo(y.Length);

                        if (retval != 0)
                       {
                               return retval;
                       }
                      else
                     {
                                return x.CompareTo(y);
                     }
            }
        }
    }

    public static void Main()
    {
        List<string> dinosaurs = new List<string>();
        dinosaurs.Add("Pachycephalosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("");
        dinosaurs.Add(null);
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Deinonychus");
        Display(dinosaurs);

        Console.WriteLine("/nSort with generic Comparison<string> delegate:");
        dinosaurs.Sort(CompareDinosByLength);
        Display(dinosaurs);

    }

    private static void Display(List<string> list)
    {
        Console.WriteLine();
        foreach( string s in list )
        {
            if (s == null)
                Console.WriteLine("(null)");
            else
                Console.WriteLine("/"{0}/"", s);
        }
    }
}

/* This code example produces the following output:

"Pachycephalosaurus"
"Amargasaurus"
""
(null)
"Mamenchisaurus"
"Deinonychus"

Sort with generic Comparison<string> delegate:

(null)
""
"Deinonychus"
"Amargasaurus"
"Mamenchisaurus"
"Pachycephalosaurus"
 */

方法三 sort(Icomparer(T)comparer)

 

如果提供了 comparer,则 List<T>的元素是使用指定的 IComparer<T> 实现进行排序的。

如果 comparernullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing,则默认比较器 Comparer<T>.Default 将检查类型 T 是否实现了 IComparable<T>泛型接口,如果实现了该接口,则使用该实现。 否则,Comparer<T>.Default 将检查类型 T 是否实现了 IComparable 接口。 如果类型 T 未实现任一接口,则 Comparer<T>.Default 将引发 InvalidOperationException。

 

using System;
using System.Collections.Generic;

public class DinoComparer: IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (x == null)
        {
            if (y == null)
            {
                // If x is null and y is null, they're
                // equal.
                return 0;
            }
            else
            {
                // If x is null and y is not null, y
                // is greater.
                return -1;
            }
        }
        else
        {
            // If x is not null...
            //
            if (y == null)
                // ...and y is null, x is greater.
            {
                return 1;
            }
            else
            {
                // ...and y is not null, compare the
                // lengths of the two strings.
                //
                int retval = x.Length.CompareTo(y.Length);

                if (retval != 0)
                {
                    // If the strings are not of equal length,
                    // the longer string is greater.
                    //
                    return retval;
                }
                else
                {
                    // If the strings are of equal length,
                    // sort them with ordinary string comparison.
                    //
                    return x.CompareTo(y);
                }
            }
        }
    }
}

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();
        dinosaurs.Add("Pachycephalosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Deinonychus");
        Display(dinosaurs);

        DinoComparer dc = new DinoComparer();

        Console.WriteLine("/nSort with alternate comparer:");
        dinosaurs.Sort(dc);
        Display(dinosaurs);

        SearchAndInsert(dinosaurs, "Coelophysis", dc);
        Display(dinosaurs);

        SearchAndInsert(dinosaurs, "Oviraptor", dc);
        Display(dinosaurs);

        SearchAndInsert(dinosaurs, "Tyrannosaur", dc);
        Display(dinosaurs);

        SearchAndInsert(dinosaurs, null, dc);
        Display(dinosaurs);
    }

    private static void SearchAndInsert(List<string> list,
        string insert, DinoComparer dc)
    {
        Console.WriteLine("/nBinarySearch and Insert /"{0}/":", insert);

        int index = list.BinarySearch(insert, dc);

        if (index < 0)
        {
            list.Insert(~index, insert);
        }
    }

    private static void Display(List<string> list)
    {
        Console.WriteLine();
        foreach( string s in list )
        {
            Console.WriteLine(s);
        }
    }
}

/* This code example produces the following output:

Pachycephalosaurus
Amargasaurus
Mamenchisaurus
Deinonychus

Sort with alternate comparer:

Deinonychus
Amargasaurus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "Coelophysis":

Coelophysis
Deinonychus
Amargasaurus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "Oviraptor":

Oviraptor
Coelophysis
Deinonychus
Amargasaurus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "Tyrannosaur":

Oviraptor
Coelophysis
Deinonychus
Tyrannosaur
Amargasaurus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "":


Oviraptor
Coelophysis
Deinonychus
Tyrannosaur
Amargasaurus
Mamenchisaurus
Pachycephalosaurus
 */

 

方法四 sort(int index,int count,Icomparer<T>Comparer)

此方法和方法三类似,只是多了两个参数,确定了在某个范围内的元素进行排序。index表示开始排序的位置,count表示结束排序的位置

你可能感兴趣的:(String,list,null,basic,insert,output)