How to sort a string array by numeric style?

后端 未结 3 1566
耶瑟儿~
耶瑟儿~ 2020-12-17 03:10

I have a filenames array, I want to sort it by numeric style, please give to me a solution.

Example1:

Original array: [name99.txt, n

3条回答
  •  时光说笑
    2020-12-17 03:42

    There may well be a managed way to do this, but I would probably just P/invoke to StrCmpLogicalW.

    [DllImport("shlwapi.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
    static extern int StrCmpLogicalW(String x, String y);    
    

    If you use this function, rather than rolling your own comparison function, you'll get the same behaviour as Explorer and other system components that use logical comparison.

    Note, however, that this will not work in environments where WinAPI is inaccessible (such as Windows Phone, Mono or Silverlight), might work differently on different systems and should be decorated with a comment so the future maintainer of your code knows why P/Invoke is used for sorting.

提交回复
热议问题