c# - how to arrange the item of a list into series arrangement -


i have list of data contains of random data combination of string , number:

list<string> data1 = new list<string>() {   "1001a",   "1002a",   "1003a",   "1004a",   "1015a",   "1016a",   "1007a",   "1008a",   "1009a", }; 

i want data arrange series this:

1001a - 1004a, 1007a - 1009a, 1015a, 1016a 

for every more 2 counts of data series output shall have "-" between first count , last count of series, other non series data added last part , separated ",".

i'd made codes arrange data series last char of it:

string get_revisionmark = "a";  var raw_serries = arrange_revisionseries.where(p => p[p.length -       1].tostring() == get_revisionmark) .orderby(p => p[p.length - 1) .thenby(p => p.substring(0, p.length - 1)).tolist(); 

just ignore last char i'd have function that, , problem arrangement of numbers, length of data not fixed. other example of output "1001a - 1005a, 301a, 32a"

i had sample of codes works fine me, me lazy code.

for (int c1 = 0; c1 < list_num.count; c1++) {     if (list_num[c1] != 0)     {          check1 = list_num[c1];          (int c2 = 0; c2 < list_num.count; c2++)          {               if (check1 == list_num[c2])               {                   list_num[c2] = 0;                   check1 += 1;                   list_series.add(arrange_revisionseries[c2]);               }          }          check1 = 0;          if (list_series.count > 2)          {              res_series.add(list_series[0] + " " +list_series[list_series.count - 1]);              list_series.clear();          }          else          {              if (list_series.count == 1)              {                  res_series.add(list_series[0]);                  list_series.clear();              }              else              {                  res_series.add(list_series[0] + "," + list_series[1]);                  list_series.clear();              }         }     } } var combine_res = string.join(",", res_series); messagebox.show(combine_res); 

this codes work fine series number ...

a possible solution (working current set of values), please follow steps below

declare class level string list

    public list<string> data_result = new list<string>(); 

create function iterate through input string list (input string declared inside, named 'data')

    public void arrangelist()     {         list<string> data = new list<string>() { "1001a", "1002a", "1003a",          "1004a", "1015a", "1016a", "1007a", "1008a", "1009a", "1017a" };         list<int> data_int = data.select(a => convert.toint32(a.substring(0,         a.length - 1))).orderby(b => b).tolist();         int initializer = 0, counter = 0;         int finalizer = 0;          foreach (var item in data_int)         {             if (initializer == 0)             { initializer = item; continue; }             else             {                 counter++;                 if (item == initializer + counter)                     finalizer = item;                 else                 {                     loglisting(initializer, finalizer);                     initializer = item;                     finalizer = item;                     counter = 0;                 }             }         }         loglisting(initializer, finalizer);     } 

create function logs result data_result string list.

    public void loglisting(int initializer, int finalizer)     {         if (initializer != finalizer)         {             if (finalizer == initializer + 1)             {                 data_result.add(initializer + "a");                 data_result.add(finalizer + "a");             }             else                 data_result.add(initializer + "a - " + finalizer + "a");          }         else             data_result.add(initializer + "a");     } 


generates result list
enter image description here

thumb-up if like


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -