Sorting sometimes has to consider suffixes. This looks more natural.
Refer to the previous article in CodeProject: Http://www.codeproject.com/Articles/22978/Implementing-the-NET-IComparer-interface-to-get-a
Then I wrote a simple, considering that the main idea is the above article, so do not do too much explanation. The code is as follows:
Using system;using system.collections;using system.collections.generic;using system.linq;using System.Text; Namespace stringorder{class Program {static void Main (string[] args) {string[] array = n EW string[] {"Sequence 11", "Sequence 11s", "Sequence 12", "Sequence 1", "Sequence 12s", "sequence 1s", "sequence 22s", "Sequence 2", "Sequence 3", "Sequence 2w", "Sequence 2q", "Sequence 22r", "23r", "22r "," 22 "," 2 "," 32 "," 42 "," 12 "," 1 "}; String[] Array2 = array. (A = A, new StringCompare1 ()). ToArray (); Console.read (); }} public class stringcompare1:icomparer<string> {public int Compare (string x, string y) {Stringparser SX = new Stringparser (x); Stringparser sy = new Stringparser (y); while (SX. Tokentype! = StringParser.ETokenType.Nothing | | Sy. Tokentype! = StringParser.ETokenType.Nothing) {if (SX. Tokentype = = StringParser.ETokenType.Numberic && sy. Tokentype = = StringParser.ETokenType.Numberic) {return (int) (SX. Doublevalue-sy. Doublevalue); } if (String.Compare (SX). StringValue, Sy. stringvalue)! = 0) {return String.Compare (SX. StringValue, Sy. StringValue); } else {sx. NextToken (); Sy. NextToken (); }} return 0; }} public class Stringparser {private string _value; Private char _curchar; private int _curindex = 0; private int _length; Private Etokentype _tokentype = Etokentype.character; Public Etokentype Tokentype {get {return _tokentype;}} private string _stringvalue; public string StringValue {get {return _stringvalue;}} Private double _doublevalue; Public double Doublevalue {get {return _doublevalue;}} Public Stringparser (String val) {_value = Val; _length = val. Length; Nextchar (); NextToken (); } public void NextToken () {if (_curchar = = ') ' {_tokentype = Etoken type.nothing; _stringvalue = null; } else if (char. IsDigit (_curchar)) {int startIndex = _curindex; while (char. IsDigit (_curchar) | | _curchar = = '. ') {Nextchar (); The string temp = _value. Substring (StartIndex-1, _length-startindex+1); if (double. TryParse (temp, out _doublevalue)) {_tokentype = Etokentype.numberic; } else {_tokentype = Etokentype.character; } _stringvalue = temp; } else if (char. Isletter (_curchar)) {_tokentype = etokentype.chAracter; int startIndex = _curindex; while (char. Isletter (_curchar)) {Nextchar (); } _stringvalue = _value. Substring (Startindex-1,_curindex-startindex); } else {Nextchar (); }} private void Nextchar () {if (_curindex >= _length) {_CU Rchar = ' + '; Return } else {_curchar = _value[_curindex]; _curindex + = 1; }} public enum Etokentype {Nothing, Character, numberic , } }}
C # implements the natural sorting effect by 1,2,11 instead of 1,11,12, distinguishing between alphabetic text and numbers