Let's talk about the problem first. Simply put, it is a set of data.
String[] data = ,,,,,,,,,
I want to sort strings and sort the strings. I should think of it as sorting by comparing the size of a single character, but the result after sorting is:
-03A02 |
101 |
-101 |
102 |
-102 |
201 |
-201 |
202 |
-202 |
301 |
-301 |
302 |
-302 |
3A01 |
-3A01 |
3A02 |
T3A01 |
First, I felt that the sorting order was wrong, because the strings starting with a negative sign should be in the same order.
Then I found the following message on MSDN:
Use the word sorting rule to perform a region-sensitive comparison. In this comparison, special weights may be assigned to non-alphanumeric Unicode characters. Using the word sorting rules and conventions in a specific region, the weight of a hyphen ("-") may be very small, therefore, "coop" and "co-op" appear next to each other in the sorting list.
Then I basically understood that in C #, the "-" negative number permission is automatically downgraded because of the English language itself, resulting in such sorting problems.
Then I looked for the following solution:
In the List <T>. sort method, you can write the sorting method by yourself, or implement a class that inherits the IComparer interface. In the Compare implementation, you can write a string statement like this:
.Compare(x, y, StringComparison.Ordinal);
The third parameter is interpreted as "comparing strings using sequence number sorting rules"
"-" Will not be automatically downgraded.
Another possibility is that there is no such sort method for us to use. For example, in DataTable, I chose this method in DataTable:
String2 : IComparable, IComparable<String2> String2(= (obj == CompareTo(= obj (other ==
First, I declare a type that inherits IComparable and IComparable <String2> and implements the CompareTo method;
Then set the column type in the DataTable to String2, so the sorting is normal.
Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------
The above are the problems and solutions encountered in string sorting. I don't know if there is any better solution. I hope you can point out the error or be a rookie.