c# - Sorting List of Strings -


i'm having list<string> l_lsttemp , contains

"a1" "a1_1" "a1_2" "1a" "b2_1" "b1_2" "b1_1_2" "a10" "b11" "a" "z" 

i need sort items based on character , numeric value.

so sorted list like

"1a" "a" "a1" "a1_1" "a1_2" "a10" "b1_1_2" "b1_2" "b2_1" "b11" "z" 

here code:

l_lsttemp.sort(delegate(string one, string two) {     match l_mone = regex.match(one, @"(\d*)(\d*)");     match l_mtwo = regex.match(two, @"(\d*)(\d*)");     int result;     if (l_mone.success || l_mtwo.success)     {         string l_strx, l_stry;         l_strx = l_mone.groups[1].value;         l_stry = l_mtwo.groups[1].value;         result = l_strx.compareto(l_stry);         if (result != 0)             return result;         l_strx = l_mone.groups[2].value;         l_stry = l_mtwo.groups[2].value;         if (l_strx == string.empty || l_stry == string.empty)         {             result = l_strx.compareto(l_stry);             if (result != 0)                 return result;         }         else         {             long x = long.parse(l_strx);             long y = long.parse(l_stry);             result = x.compareto(y);             if (result != 0)                 return result;         }      }     return 0 ; } ); 

but not working (sorting) properly.

how modify code sort list properly?

please post me way this.

thanks in advance.

i did modifications code. thing when both group 1 , group 2 equals, still need check remains.

important: did modifications inside code, little tricky. really suggest you refactoring code know works:

l.sort(delegate(string one, string two) {     while (one != "" && 2 != "")     {         if (one == two)             return 0;          //add 1 more group capture remains of expression         match l_mone = regex.match(one, @"_*(\d*)(\d*)(.*)$");          match l_mtwo = regex.match(two, @"_*(\d*)(\d*)(.*)$");         int result;         if (l_mone.success || l_mtwo.success)         {             string l_strx, l_stry;             l_strx = l_mone.groups[1].value;             l_stry = l_mtwo.groups[1].value;             result = l_strx.compareto(l_stry);             if (result != 0)                 return result;             l_strx = l_mone.groups[2].value;             l_stry = l_mtwo.groups[2].value;             if (l_strx == string.empty || l_stry == string.empty)             {                 result = l_strx.compareto(l_stry);                 if (result != 0)                     return result;             }             else             {                 long x = long.parse(l_strx);                 long y = long.parse(l_stry);                 result = x.compareto(y);                 if (result != 0)                     return result;                 1 = l_mone.groups[3].value; //store in 'one' remaining part of regex                 2 = l_mtwo.groups[3].value; //the same in 2                  continue; //the result result of comparison of 2 new values.             }          }     }     return one.compareto(two); }); 

edit:
added _* remove _ characters begining of strings. assumed here strings contain _ after numbers , not b1b or b1$.

the thing here don't explain how comparison should made, , had assume things original data , sorted data, otherwise happen if want sort a1a , a1_? should return?


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -