c# - Array list is not being sorted properly when it contains string -




i'm trying sort lists of alphanumeric values , in other words list contains numbers , strings

example : bob10, bot20, etc...

list<object> mylist = _items.orderby(x => x.firstname).tolist(); _items= new list<object>(mylist); 

but still output : bot20 , bob10

what wrong?

first : try alphanumeric algorithm approach , first implement algorithm in new class, :

list<object> yourlist = new list < object >(thepreviouslist.orderby(x => x.firstname, new alphanumcomparatorfast()).tolist())  

as explained here :

http://www.dotnetperls.com/alphanumeric-sorting

implement algorithm alphanumcomparatorfast creating new class , call

edited :

the alpha algo below :

public class alphanumcomparatorfast : icomparer {     public int compare(object x, object y)     {         string s1 = x string;         if (s1 == null)         {             return 0;         }         string s2 = y string;         if (s2 == null)         {             return 0;         }          int len1 = s1.length;         int len2 = s2.length;         int marker1 = 0;         int marker2 = 0;          // walk through 2 strings 2 markers.         while (marker1 < len1 && marker2 < len2)         {             char ch1 = s1[marker1];             char ch2 = s2[marker2];              // buffers can build characters in each chunk.             char[] space1 = new char[len1];             int loc1 = 0;             char[] space2 = new char[len2];             int loc2 = 0;              // walk through following characters digits or             // characters in both strings starting @ appropriate marker.             // collect char arrays.                         {                 space1[loc1++] = ch1;                 marker1++;                  if (marker1 < len1)                 {                     ch1 = s1[marker1];                 }                 else                 {                     break;                 }             } while (char.isdigit(ch1) == char.isdigit(space1[0]));                          {                 space2[loc2++] = ch2;                 marker2++;                  if (marker2 < len2)                 {                     ch2 = s2[marker2];                 }                 else                 {                     break;                 }             } while (char.isdigit(ch2) == char.isdigit(space2[0]));              // if have collected numbers, compare them numerically.             // otherwise, if have strings, compare them alphabetically.             string str1 = new string(space1);             string str2 = new string(space2);              int result;              if (char.isdigit(space1[0]) && char.isdigit(space2[0]))             {                 int thisnumericchunk = int.parse(str1);                 int thatnumericchunk = int.parse(str2);                 result = thisnumericchunk.compareto(thatnumericchunk);             }             else             {                 result = str1.compareto(str2);             }              if (result != 0)             {                 return result;             }         }         return len1 - len2;     } } 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -