I have a List which is being returned from a WCF service ,for List of files on Server. Which i am later using to populate the TreeView in my Client Application
I need the list to be in naturally sorted order.
For ex : I have a list something like
List-1
, and my expected result isList-2
but i getList-3
as my output.(refer list given below)
List-1 List-2 List-3
abc.jpg abc.jpg abc.jpg
abc10.jpg abc10.jpg abc10.jpg
abc100.jpg abc97.jpg abc100.jpg
abc98.jpg abc98.jpg abc101.jpg
abc97.jpg abc100.jpg abc102.jpg
abc102.jpg abc101.jpg abc97.jpg
abc101.jpg abc102.jpg abc98.jpg
So far i have looked into the stackoverflow's post :
[1] :C# Sort files by natural number ordering in the name? [2]: Sorting a FileInfo[] based using Natural Sorting on the filename (SQL files) .
None of them found to working for my case , Any help would be appreciated :)
Here you go; a handy list extension for natural sorting:
The output from this program is:
This takes advantage of the Windows API
StrCmpLogicalW()
method which does a natural sort order comparison, and uses P/Invoke to call it.