How to sort a string array from maximum to minimum amount of that type of elements in c#? -


i sorry confusing title, how make array of these elements :

string [] anarray = new string[50]; anarray[0] = "new york"; anarray[1] = "london"; anarray[2] = "new york"; anarray[3] = "london"; anarray[4] = "new york"; anarray[5] = "chicago"; 

fall array :

anarray[0] = "new york"; anarray[1] = "new york"; anarray[2] = "new york"; anarray[3] = "london"; anarray[4] = "london"; anarray[5] = "chicago"; 

where elements sorted amount of equal elements there in array. , if example anarray has equal amount of elements :

anarray[0] = "new york"; anarray[1] = "new york"; anarray[2] = "london"; anarray[3] = "london"; 

how make program finds both of these elements , outputs like: new york, amount of elements 2 london, amount of elements 2

i'll hope understand, sorry if may confusing, here.

just use linq groupby

        string[] anarray = new string[50];         anarray[0] = "new york";         anarray[1] = "london";         anarray[2] = "new york";         anarray[3] = "london";         anarray[4] = "new york";         anarray[5] = "chicago";          var groupedarray = anarray.groupby(a => a);         foreach (var item in groupedarray)         {             console.writeline(item.key + " -> " + item.count());         }         console.read();          //newyork -> 3         //london -> 2         //chicago -> 1 

on side note, initialized array hold 50 items in example result in 44 empty string items being taken account groupby clause. if want avoid displaying these, replace groupby line one:

var groupedarray = anarray.groupby(a => a).where(a => !string.isnullorempty(a.key)); 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -