justangel

c# linq orderby numbers that are string

Nov 8th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         string[] str = { "1_djjf", "12_shdfkj", "2_sdfsdf", "1112_sdfd", "10_blablabla" };
  11.  
  12.         str = str.OrderBy(x => x, new NaturalSort()).ToArray();
  13.  
  14.         foreach (var line in str)
  15.             Console.WriteLine(line);
  16.     }
  17. }
  18.  
  19. public class NaturalSort : IComparer<string>
  20. {
  21.     [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
  22.     public static extern int StrCmpLogicalW(string x, string y);
  23.  
  24.     public int Compare(string x, string y)
  25.     {
  26.         return StrCmpLogicalW(x, y);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment