C# 泛型List排序的实现

本文主要介绍了C# 泛型List排序的实现,分享给大家,具体如下:

代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace L_List_sort

{

    public class Person:IComparable<Person>

    {

        // 属性

        public string name;

        public int age;

        // 构造

        public Person(string name, int age)

        {

            this.name = name;

            this.age = age;

        }

        // 重写字符串

        public override string ToString()

        {

            return "name: " + this.name + "  age: " + this.age;

        }

        // 实现比较接口

        public int CompareTo(Person other)

        {

            // 根据返回值排序  升序

            if (this.age > other.age)

            {   // 大于0 放后面

                return 1;

            }

            else

            {   // 小于 0 放前面

                return -1;

            }

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("泛型数组的排序");

            #region 知识一 List自带排序方法

            Console.WriteLine("--------------------List自带排序方法");

            List<int> listInt = new List<int>();

            listInt.Add(4);

            listInt.Add(2);

            listInt.Add(3);

            listInt.Add(1);

            Console.WriteLine("-------排序前");

            PrintList<int>(listInt);

            Console.WriteLine("-------排序后");

            // 排序

            listInt.Sort();

            PrintList<int>(listInt);

            #endregion

            #region 知识二 自定义类的排序

            Console.WriteLine("--------------------自定义类的排序");

            List<Person> listPerson = new List<Person>();

            listPerson.Add(new Person("张三", 20));

            listPerson.Add(new Person("李四", 18));

            listPerson.Add(new Person("王五", 31));

            listPerson.Add(new Person("曹操", 45));

            Console.WriteLine("-------排序前");

            PrintList<Person>(listPerson);

            Console.WriteLine("-------排序后");

            // 继承排序(需要继承 接口 :IComparable<Person>)

            listPerson.Sort();

            PrintList<Person>(listPerson);

            #endregion

            #region 知识三 通过委托函数进行排序

            Console.WriteLine("--------------------通过委托函数进行排序");

            listPerson.Clear();

            listPerson.Add(new Person("张三", 20));

            listPerson.Add(new Person("李四", 18));

            listPerson.Add(new Person("王五", 31));

            listPerson.Add(new Person("曹操", 45));

            Console.WriteLine("-------排序前");

            PrintList<Person>(listPerson);

            // 使用委托==>函数排序

            listPerson.Sort(SortPerson);

            Console.WriteLine("-------排序后");

            PrintList<Person>(listPerson);

            // Lambda 再次排序

            listPerson.Sort((leftP, rightP) => {

                return leftP.age > rightP.age ? 1 : -1;

            });

            

            Console.WriteLine("-------Lambda 再次排序后");

            PrintList<Person>(listPerson);

            #endregion

            Console.ReadLine();

        }

        // 排序函数

        private static int SortPerson(Person leftP, Person rightP)

        {

            // 根据返回值排序  升序

            if (leftP.age > rightP.age)

            {   // 大于0 放后面

                return -1;

            }

            else

            {   // 小于 0 放前面

                return 1;

            }

        }

        // 打印列表中元素的内容

        private static void PrintList<T>(List<T> nList)

        {

            if (nList.Count == 0)

                Console.WriteLine("--列表为空数据");

            for (int i = 0; i < nList.Count; i++)

            {

                Console.WriteLine(nList[i].ToString());

            }

        }

    }

}

猜你喜欢

转载自blog.csdn.net/qq_15509251/article/details/131536014