Hi....now I need some helps..because I totally no idea to do that..
Now, I need to read and count the frequency for each items
Eg,
set of data in text file
1 2
1 3
1 4
2 3
3 4

Now, i wan to read each item and count the frequency for each item.
ITEM FREQUENCY
1 contain 3
2 contain 2
3 contain 3
4 contain 2

after that,I need to rearrange my data.I have to sort each item in each row from highest frequency to lowest.
Example result,
1 2
1 3
1 4
3 2
3 4
2 3(red) will sort to 3 2 because 3 is more frequent than the 2.
Hope someone can give me some idea to do this... thanks ya..

set of data in text file
1 2
1 3
1 4
2 3
3 4

Now, i wan to read each item and count the frequency for each item.
ITEM FREQUENCY
1 contain 3
2 contain 2
3 contain 3
4 contain 2

Where did you get this last table? How, explain more? 1 contaions 3 ? whats is that suppose to mean?
Mitja

Where did you get this last table? How, explain more? 1 contaions 3 ? whats is that suppose to mean?
Mitja

I get after my combination. 1 contain 3 is mean in that set of data 1 appear 3 times.
Now i want to read and count the set of data that i gave in the above post.

key
1 2
1 3
1 4
2 3
2 4
3 4

now i want to count each items...but i don't have any ideas to do that...

Whaz I dont understand also, if the sorting (yout last request). I dont get it why you changed the 3 and 2.
Maybe would be a good idea to explain what all these numbers mean, and what is the connection between them.

I will add additional row to ask you if I understand this correctly:
Lets say we have on beginning:
1 2
1 3
1 4
2 3
3 4
5 3

The output would be:
1 2
3 1 //here chnages, becuase 3 has 4 prepetitions
1 4
3 2 //here too
3 4
3 5 //and here

Am I right?

Edited 5 Years Ago by Mitja Bonca: n/a

Man I did it - finally. It works like charm.
Here`s the code:

class Program
    {
        static void Main(string[] args)
        {
            List<Values> list = new List<Values>();
            Dictionary<int, int> dic = new Dictionary<int, int>();

            using (StreamReader sr = new StreamReader(@"C:\1\test22.txt"))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] _split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    Values v = new Values();
                    for (int i = 0; i < _split.Length; i++)
                    {
                        int number = Convert.ToInt32(_split[i]);

                        //1. adding a number to common list<T>:
                        if (i == 0)
                            v.Value1 = number;
                        else
                            v.Value2 = number;

                        //2. adding number to a dictionary:
                        if (dic.ContainsKey(number))
                            dic = dic.ToDictionary(d => d.Key, d => d.Key == number ? d.Value + 1 : d.Value);
                        else
                            dic.Add(number, 1);
                    }
                    //1.1 adding values of both numbers to the list<T>:
                    list.Add(v);
                }
            }

            //arranging the array:
            foreach (Values v in list)
            {
                int value1 = v.Value1;
                int value2 = v.Value2;
                int value1Rep = 0;
                int value2Rep = 0;                
                foreach (KeyValuePair<int, int> kvp in dic)
                {
                    if (kvp.Key == value1)
                        value1Rep = kvp.Value;
                    else if (kvp.Key == value2)
                    {
                        value2Rep = kvp.Value;
                        break;
                    }
                }
                if (value2Rep > value1Rep)
                {
                    v.Value1 = value2;
                    v.Value2 = value1;
                }

                //show the result:
                Console.WriteLine("{0} - {1}", v.Value1, v.Value2);
            }
            Console.ReadLine();
        }       
    }
    class Values
    {
        public int Value1 { get; set; }
        public int Value2 { get; set; }
    }

Tell me what do you think.
Mitja

U concept is what i want to say..But i use string to my program and do not use List<T> and Dictionary...

i want to ask how to display the result using message box..? because i tried many times the result display using message box.

TI have converted the app. to Win form application. Now the result is shown in messageBox:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Mar24NumberSorting_Win
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Method();
        }

        private void Method()
        {
            List<Values> list = new List<Values>();
            Dictionary<int, int> dic = new Dictionary<int, int>();

            string strPath = @"C:\1\test22.txt";
            using (StreamReader sr = new StreamReader(strPath))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] _split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    Values v = new Values();
                    for (int i = 0; i < _split.Length; i++)
                    {
                        int number = Convert.ToInt32(_split[i]);

                        //1. adding a number to common list<T>:
                        if (i == 0)
                            v.Value1 = number;
                        else
                            v.Value2 = number;

                        //2. adding number to a dictionary:
                        if (dic.ContainsKey(number))
                            dic = dic.ToDictionary(d => d.Key, d => d.Key == number ? d.Value + 1 : d.Value);
                        else
                            dic.Add(number, 1);
                    }
                    //1.1 adding values of both numbers to the list<T>:
                    list.Add(v);
                }
            }

            //arranging the array:

            string allText = null;
            foreach (Values v in list)
            {
                int value1 = v.Value1;
                int value2 = v.Value2;
                int value1Rep = 0;
                int value2Rep = 0;
                foreach (KeyValuePair<int, int> kvp in dic)
                {
                    if (kvp.Key == value1)
                        value1Rep = kvp.Value;
                    else if (kvp.Key == value2)
                    {
                        value2Rep = kvp.Value;
                        break;
                    }
                }
                if (value2Rep > value1Rep)
                {
                    v.Value1 = value2;
                    v.Value2 = value1;
                }

                //show the result:
                allText += v.Value1 + " - " + v.Value2 + Environment.NewLine;
            }
            MessageBox.Show("This is the result:\n\n" + allText);

        }
    }

    class Values
    {
        public int Value1 { get; set; }
        public int Value2 { get; set; }
    }
}

btw, I strongly suggest you to use Colections, like Dictionary and generic List<T> in such cases. You will make your life WAAAAY easier. Dont even think of doing such a code with a simple using of objects. Believe me.
Slowly examin my code, and do some other examples that you get a touch of how it works and so on. But it will take time to undersand all.

Hope it helps,
Mitja

How you cannot run? Whats the matter? I cannot understand what is wrong, becuase it should work. Please tell me.
Mitja

How you cannot run? Whats the matter? I cannot understand what is wrong, becuase it should work. Please tell me.
Mitja

Now, my program contain Key and Value.I thought can do separately but is wrong. I need to rearrange the Items of key without touch the Value..

for example,
Key Value
1 2 2
1 2 3 1
2 3 2
2 4 1
2 4 5 2

Now, i wan to read each item and count the frequency for each item.
Item Frequency
1 2
2 5
3 2
4 2
5 1

After that,I have to sort each item in each row from highest frequency to lowest.
Key Value
2 1 2
2 1 3 1
2 3 2
2 4 1
2 4 5 2

I want to do like this....So now, I stuck ..

Man I did it - finally. It works like charm.
Here`s the code:

class Program
    {
        static void Main(string[] args)
        {
            List<Values> list = new List<Values>();
            Dictionary<int, int> dic = new Dictionary<int, int>();

            using (StreamReader sr = new StreamReader(@"C:\1\test22.txt"))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] _split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    Values v = new Values();
                    for (int i = 0; i < _split.Length; i++)
                    {
                        int number = Convert.ToInt32(_split[i]);

                        //1. adding a number to common list<T>:
                        if (i == 0)
                            v.Value1 = number;
                        else
                            v.Value2 = number;

                        //2. adding number to a dictionary:
                        if (dic.ContainsKey(number))
                            dic = dic.ToDictionary(d => d.Key, d => d.Key == number ? d.Value + 1 : d.Value);
                        else
                            dic.Add(number, 1);
                    }
                    //1.1 adding values of both numbers to the list<T>:
                    list.Add(v);
                }
            }

            //arranging the array:
            foreach (Values v in list)
            {
                int value1 = v.Value1;
                int value2 = v.Value2;
                int value1Rep = 0;
                int value2Rep = 0;                
                foreach (KeyValuePair<int, int> kvp in dic)
                {
                    if (kvp.Key == value1)
                        value1Rep = kvp.Value;
                    else if (kvp.Key == value2)
                    {
                        value2Rep = kvp.Value;
                        break;
                    }
                }
                if (value2Rep > value1Rep)
                {
                    v.Value1 = value2;
                    v.Value2 = value1;
                }

                //show the result:
                Console.WriteLine("{0} - {1}", v.Value1, v.Value2);
            }
            Console.ReadLine();
        }       
    }
    class Values
    {
        public int Value1 { get; set; }
        public int Value2 { get; set; }
    }

Tell me what do you think.
Mitja

I try your code already.Its work.Thank ya...But, now i got many item now..i no idea to change them
for example:
Key
1 2
1 2 3
1 2 3 4
1 2 4
2 3
2 4 5
2 4 5 6
.....
if like this, how i sort them in highest to lowest..You got any idea?

Just follow the pattern I did for 2 "column". Add new property in the class, and re-work the code in the Dictionary.
No big deal.

Try mate, try. Get some books or something. Otherwise you will never learn. I showed you how it needs to be done. You simply study my example and add whats needed.
Come on...

okok.....thanks ya...because now i am rushing to pass up this project..

Try mate, try. Get some books or something. Otherwise you will never learn. I showed you how it needs to be done. You simply study my example and add whats needed.
Come on...

I really do not know how to change your code to add in my program..now stuck already...

Please mark this thread as answered, because it was ANSWERED already (at least regarding the thread`s title).
And get serious (and books too).
Mitja

This article has been dead for over six months. Start a new discussion instead.