Sorting strings....sorting them properly?
-
Hi all. I'm working on a custom listview control, developed in C#. I'm currently trying to implement the sorting algorithm so when someone clicks on a header, it gets properly sorted. My current approach is to use a quicksort with the .NET String.Compare method. This works some of the time, but there are several cases where it is seriously lacking. For one, performance is crap. Second, when there are numbers in the strings, the sorting gets all screwed up because String.Compare only does lexical checking, and doesn't take into account numbers. I'm wondering if anyone has any links to resources on sorting strings with numbers (sometimes its called natural sorting). I have found a few papers on the subject, but most are very old, and use C and memory allocation. My control is developed using C#, and the low-level memory access of C doesn't translate well into C#, even with an unsafe block. I appreciate any help, links, or advice.
-
Hi all. I'm working on a custom listview control, developed in C#. I'm currently trying to implement the sorting algorithm so when someone clicks on a header, it gets properly sorted. My current approach is to use a quicksort with the .NET String.Compare method. This works some of the time, but there are several cases where it is seriously lacking. For one, performance is crap. Second, when there are numbers in the strings, the sorting gets all screwed up because String.Compare only does lexical checking, and doesn't take into account numbers. I'm wondering if anyone has any links to resources on sorting strings with numbers (sometimes its called natural sorting). I have found a few papers on the subject, but most are very old, and use C and memory allocation. My control is developed using C#, and the low-level memory access of C doesn't translate well into C#, even with an unsafe block. I appreciate any help, links, or advice.
Hi Jon, I'm using a custom ColumnSorter on my ListView and I use String.Compare without any problems. I haven't seen any performance issues though I haven't tried sorting any lists with more than 30 or so entries so far. And I just ran through some test cases with numbers in the strings and they worked as I would expect. Can you post some snippets of code or maybe more specific problems that you're seeing? Michael CSO, ActiveWare LLC http://www.ActiveWareSolutions.com
-
Hi Jon, I'm using a custom ColumnSorter on my ListView and I use String.Compare without any problems. I haven't seen any performance issues though I haven't tried sorting any lists with more than 30 or so entries so far. And I just ran through some test cases with numbers in the strings and they worked as I would expect. Can you post some snippets of code or maybe more specific problems that you're seeing? Michael CSO, ActiveWare LLC http://www.ActiveWareSolutions.com
Well, I'm using String.Compare as part of a quicksort algorithm. It sorts strings, but when there are numbers in strings, they are treated lexically, rather than numerically. For example, strings with numbers are sorted the following way: string1 string10 string11 string2 string21 string3 string4 string45 string489 string49 string5 string6 string7 string71 I need strings to be sorted "properly" when they have numbers in them, like follows: string1 string2 string3 string4 string5 string6 string7 string10 string11 string21 string45 string49 string71 string489 I havn't been able to find an efficient way of performing such a search. While I have found numerous web sites that refer to "natural" string sorting, in which number values are taken into account when sorting strings, I have found no examples of such algorithms. My first attempt created one of the most inefficient sorting times I have ever seen, and I'm sure there is a better way. Thanks for any help.