none
String Compare problem RRS feed

  • Question

  • When compare two strings with function String.Compare as "file.txt" and "file 1.txt" we get result that "file 1.txt" less then "file.txt". It leads to incorrect sorting of filenames. 

    For example now in Explorer "file.txt" in the bottom, earlier it was first.

    file1.txt

    file2.txt

    file.txt


    • Edited by dvidja Tuesday, April 30, 2019 9:39 AM
    Tuesday, April 30, 2019 9:29 AM

All replies

  • Check out the top 2 answers on StackOverflow on how to do this.

    https://stackoverflow.com/questions/40759631/sort-list-items-full-path-by-file-name-in-c-sharp


    william xifaras

    Tuesday, April 30, 2019 6:05 PM
  • Hi dvidja,

    Thank you for posting here.

    For your question, you use String.Compare for "file.txt", "file 1.txt". And I am confused about the results you want to get like below.

    file1.txt
    file2.txt
    file.txt

    The code would not return the things you want to get.

     string.Compare("file.txt", "file 1.txt");

    Please provide more details. Code with error message and the result with explanation would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 1, 2019 5:14 AM
    Moderator
  • Hello

    Sorry, it was my mistake. Comparing strings work correct. I tried to compare filenames with extensions and it leaded to incorrect sorting.

    Tuesday, May 7, 2019 12:21 PM
  • Hi dvidja,

    The filenames are still strings. It should be what I provided in my previous reply.

    Could you provide your code with error message for us to test?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 9, 2019 2:10 AM
    Moderator