日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

排序集合的一个小坑

發(fā)布時間:2023/12/4 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 排序集合的一个小坑 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原來一直用SortList,SortedDictionary來作為鍵值對存儲的排序集合來用,心中就默認(rèn)是以key按ascall排序來存放的,在之前的案例中也沒有出現(xiàn)問題,在最近一個demo中,打破了原來的自以為是的認(rèn)識,因為在key中不但有大寫小,還有特列符號。

先看一下代碼:

Console.WriteLine("-----------按ASCII排序-----------"); var chars = new char[] { 'A', '[', ']', 'a' }; foreach (var c in chars) {Console.WriteLine($"{c}:{(int)c}"); } Console.WriteLine("-----------排序集合的排序-----------"); var list = new SortedList<string, int>(); list.Add("a", 97); list.Add("A", 65); list.Add("[", 91); list.Add("]", 93); foreach (var item in list) {Console.WriteLine($"{item.Key}:{item.Value}"); }

結(jié)果如下,顯然SortList的key結(jié)果不是想要的按ascall排序的。

那怎么才能達(dá)到按ascall呢?那就自己動手做一個排序器吧,其實(shí)就是實(shí)現(xiàn)IComparer<string>接口中的Compare,告訴兩個string的比較規(guī)則,那自然多個數(shù)據(jù)的排序就能按這種規(guī)則給出來。當(dāng)然我給的按ascall的這個規(guī)則,絲毫沒有優(yōu)美而言,只是能表示出意思來。

Console.WriteLine("-----------新排序集合的排序-----------"); var newList = new SortedList<string, int>(new ASCALLComparer()); newList.Add("a", 97); newList.Add("A", 65); newList.Add("[", 91); newList.Add("]", 93); foreach (var item in newList) {Console.WriteLine($"{item.Key}:{item.Value}"); }public class ASCALLComparer : IComparer<string> {public int Compare(string? x, string? y) {if (x == null || y == null){throw new Exception("x or y is null");}if (x?.Length != y?.Length){if (x?.Length < y?.Length){for (var i = 0; i < x?.Length; i++){if ((int)x[i] > (int)y[i]){return 1;}else if ((int)x[i] < (int)y[i]){return -1;}}return -1;}else{for (var i = 0; i < y?.Length; i++){if ((int)x[i] > (int)y[i]){return 1;}else if ((int)x[i] < (int)y[i]){return -1;}}return 1;}}else{for (var i = 0; i < x?.Length; i++){if ((int)x[i] > (int)y[i]){return 1;}else if ((int)x[i] < (int)y[i]){return -1;}}return 0;}} }

結(jié)果為:

那原來的排序規(guī)則是什么呢?我枚舉了一下ascall范圍內(nèi)部分可見字符,下面是正序的排序方式:

序號
符號
ascall值

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

_

-

,

;

:

!

?

.

'

"

(

)

[

]

{

}

@

*

/

\

&

#

%

`

^

+

<

=

>

|

~

$

0

1

2

3

4

5

6

7

8

9

a

A

b

B

c

C

d

D

e

E

f

F

g

G

h

H

i

I

j

J

k

K

l

L

m

M

n

N

o

O

p

P

q

Q

r

R

s

S

t

T

u

U

v

V

w

W

x

X

y

Y

z

Z

95

45

44

59

58

33

63

46

39

34

40

41

91

93

123

125

64

42

47

92

38

35

37

96

94

43

60

61

62

124

126

36

48

49

50

51

52

53

54

55

56

57

97

65

98

66

99

67

100

68

101

69

102

70

103

71

104

72

105

73

106

74

107

75

108

76

109

77

110

78

111

79

112

80

113

81

114

82

115

83

116

84

117

85

118

86

119

87

120

88

121

89

122

90

總結(jié)

以上是生活随笔為你收集整理的排序集合的一个小坑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。