c# - Sorting IEnumerable<string> -


this question has answer here:

i've got ienumerable<string> values like:

  • pp
  • pp.10.0
  • pp.6.0
  • pp.6.1
  • pp.8.3
  • kp.10.1
  • kp
  • kp.3.20

... etc

i'd organize them alphabetically, keeping values double digits (in example above ones 10.0 , 10.1) below ones single digits. in other words, after sorting should this:

  • kp
  • kp.3.20
  • kp.10.1
  • pp
  • pp.6.0
  • pp.6.1
  • pp.8.3
  • pp.10.0

i tried simple ienumerable<string>.orderby(x => x, stringcomparer.currentcultureignorecase) doesn't quite work in case.

also bear in mind pp , kp parts could have dots in them well, if you're thinking of splitting string.

just create own comparer , pass overload of orderby: https://msdn.microsoft.com/de-de/library/bb549422(v=vs.110).aspx

for example:

ienumerable<string>.orderby(x => x, new mycomparer()); 

your own comparer can class (in example above named mycomparer) implements interface system.collections.generic.icomparer<string>. put compare logic method compare.

class mycomparer : icomparer<string> {     public int compare(string x, string y)     {         // logic here     } }  // ...  ienumerable<string> mylistofstrings = ...;  mylistofstrings.orderby(x => x, new mycomparer()); 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -