Problems and Solutions of string sorting in C #

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.