LINQ sorting based on text a-001-002-003 this type to split sort the null value is placed in the last

Source: Internet
Author: User

Call

1list<string> data =Newlist<string>() {2                 "d-001-001-001","a-001-004-001","a-001-002-001",3                 "a-002-001-001","f-004-001-001","a-003-001-001",4                 "a-004-001-004","a-041-0401-031","" };5             varGF = ( fromEinchDataSelecte). (x = x,NewAlphanumericcomparer ()). ToList ();

Comparator

1    Public classalphanumericcomparer:icomparer<string>2     {3          Public intCompare (stringFirststringsecond)4         {5              intFirstnumber, Secondnumber;//define value type return values6             if(string. IsNullOrEmpty (first))7             {8                 return-1;9             }Ten             if(string. IsNullOrEmpty (second)) One             { A                 return-1; -             } -             //Split String the             string[] Firstarray = first. Split ('-'); -             string[] Secondarray = second. Split ('-'); -list<int> Intarray =Newlist<int>(); -             //Record comparison results +              for(inti =0; i < firstarray.length; i++) -             { +                 BOOLFirstisnumber =int. TryParse (Firstarray[i], outfirstnumber); A                 BOOLSecondisnumber =int. TryParse (Secondarray[i], outsecondnumber); at                 if(Firstisnumber &&secondisnumber) -                 { -                     //Number Comparison - Intarray.add (Firstnumber.compareto (Secondnumber)); -                 } -                 Else { in                     //Text comparison - Intarray.add (Firstarray[i]. CompareTo (Secondarray[i])); to                 } +             } -Intarray.reverse ();//Calculate the difference from the bottom of the number the             intval =0; *             //Get the difference ratio $              for(inti =0; i < Intarray.count; i++)Panax Notoginseng             { -Val + = intarray[i] * (i+1) *Ten; the             } +             returnVal; A         } the}

:

LINQ sorting based on text a-001-002-003 this type to split sort the null value is placed in the last

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.