sorting array list inmy program

Posted by Abhisekjani under C# on 3/28/2011 | Points: 10 | Views : 1462 | Status : [Member] | Replies : 1
Hey....I face some problem in counting the repeated times of items...hope somebody can helps me..

For example:
I have a list of data that store in string array
1
2
1
2
3
1
2
3
2
2
3
4
2
4
5
2
3
.........
(maybe have different numbers)
now i wan to count each item repeated times
item values
1 3
2 5
3 4
4 2
5 1
after that, i need to sort them based on the values from highest to lowest.
item values
2 5
3 4
1 3
4 2
5 1
this is the output that i expected.Hope someone will help me..thanks ya.
here is my coding:

String[] sArray = new String[showFile1.Lines.Length];
int p = 0;
foreach (String k in listItem.Keys)
{
sArray[p] = k;
show += sArray[p]+"\n";
p++;
} //MessageBox.Show(show.ToString());

KeyItems1 = show.Split(' ');
String display = "";
ArrayList list = new ArrayList();
foreach (string h in KeyItems1)
{
display += h + "\n";

}
list.Add(display);
String display2 = "";
foreach (string i in list)
{
display2 += i ;
}
//MessageBox.Show(display2.ToString());

String[] sArray2 = list.ToArray(typeof(String)) as String[];
String display3 = "";
foreach (String value in sArray2)
{
display3 += value;
}




Responses

Posted by: T.saravanan on: 3/28/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Try this...here i am using ArrayList for input and SortedList for output...
NameSpace:  Using System.Collections;


SortedList srList = new SortedList();
int start = 0;
for(int x=0; x< arr.Count; x++)
{
if(!srList.ContainsKey(arr[x]))
{
start = 0;
for(int y = x + 1 ; y < arr.Count + 1; y++)
{
if(y < arr.Count)
{
if(arr[x].Equals(arr[y]))
start++;
}
else
{
srList.Add(arr[x], start);
}
}
}
}


Now 'srList' comes ascending order based on key value.What ever you want change the order in 'srList'

Cheers :)

Thanks,
T.Saravanan

Abhisekjani, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response