hi people i really need help in this.
i have a c# win form which helps me in taking database from access, putting it into an array and counting the word based on user search. Each form will contain different number of words that the user search so anyone know how to rank the count at the end in descending form.

private void btnSearch_Click(object sender, EventArgs e)
        {
            cmd = new OleDbCommand(sqlstr, con);
            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                int i = 0;
                string words = "";
                words = (reader[2].ToString());
                string[] sep = { "," };
                string[] names = keywords.Split(sep,   StringSplitOptions.RemoveEmptyEntries);
                
                int count = 0;
                for (i = 0; i < names.Length; i++)
                {
                    if (names[i].Contains(searchtext.text))
                    {
                        count++;
                    }

                }
                if (count != 0)
                {
                   outputLabel.Text += reader[1].ToString() + " = " + reader[2].ToString() + "\n\n Number of word searched:" + count + "\n\n";
                }
            }
        }

currently this is my code. i tried putting the count into an arraylist and sort it from there but it does not work. so anybody has any idea.

i tried putting the count into an arraylist and sort it from there but it does not work

Could you show us the code that did not work?

First please use Code tags.
Second, make class to handle this

class MyWord //may implement some interfaces
{
string word;
public string Word { get; set;}

int rank;
public int Rank { get; set;} 
}

Then when some search meets increase the rank.

Good morning from Atlanta Danny and Ramy :)

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmSort : Form
  {

    internal class MyWord : IComparable 
    {
      public string Word { get; set; }
      public int Rank { get; set; }

      public MyWord()
      {
      }
      public MyWord(string Word)
        : this()
      {
        this.Word = Word;
        this.Rank = 1;
      }

      #region IComparable Members
      public int CompareTo(object obj)
      {
        if (obj == null)
          throw new ArgumentNullException("obj");

        if (obj is MyWord)
        {
          return this.Rank.CompareTo((obj as MyWord).Rank);
        }
        else
          throw new ArgumentException(
            string.Format("Cannot compare types MyWord and {0}", obj.GetType()),
            "obj");
      }
      #endregion
    }

    public frmSort()
    {
      InitializeComponent();
    }

    public static string BuildSqlNativeConnStr(string server, string database)
    {
      return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", server, database);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      string connStr = BuildSqlNativeConnStr("apex2006sql", "Leather");
      const string query = @"Select Top 500 * From Customer (NOLOCK) Where Name Like '%[a-Z]%,%[a-Z]%' and Name Not Like ',%' and Len(Name) >= 15 Order By Name";
      Dictionary<string, MyWord> dict = new Dictionary<string, MyWord>(StringComparer.CurrentCultureIgnoreCase);
      List<MyWord> lst = new List<MyWord>();
      using (SqlConnection conn = new SqlConnection(connStr))
      {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
          
          using (SqlDataReader dr = cmd.ExecuteReader())
          {
            while (dr.Read())
            {
              string text = Convert.ToString(dr["Name"]);
              if (string.IsNullOrEmpty(text))
                continue;
              string[] words = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
              foreach (string word in words)
              {
                string key = word.Trim();
                MyWord myWord;
                if (dict.TryGetValue(key, out myWord))
                  myWord.Rank++;
                else
                {
                  myWord = new MyWord(key);
                  dict.Add(key, myWord);
                  lst.Add(myWord);
                }
              } //foreach word
            } //while read
          } //execute reader
        } //cmd
      } //conn
      lst.Sort();
      //This gives ascending order, you can do descending, or change the implementation of IComparable
      foreach (MyWord output in lst)
      {
        Console.WriteLine(
          string.Format("Word: {0} - Count: {1:F0}", output.Word, output.Rank)
          );
      }
    }
  }
}

Scott you wrote too much again :)
i would put all the strings in a List<string> then use the sort method to do that sorting. in c# there cant be any sorting problem. that functionality is already encapsulated in many components and classes.

thanks for the reply. hmm i am trying to sort the count so i actually use

List<int> Counting = new List<int>();
Counting.Add(count);
Counting.Sort();
Foreach(int c in Counting)
{
console.Writeline(c)
}

but it does not seems to work because i am using oledbdatareader? so how do i actually sort the count ++ because my c output contains more than one count for each row.

you can use datatable or dataview, i think dataview object is better if you have multiple columns in a row. i dont try to understand what you try to do for the time being as i am writing a reseach paper, but you can sort anything with dataview object, if you want to convert dataview to datatable you can search my codesnippets for c#.

you can use datatable or dataview, i think dataview object is better if you have multiple columns in a row. i dont try to understand what you try to do for the time being as i am writing a reseach paper, but you can sort anything with dataview object, if you want to convert dataview to datatable you can search my codesnippets for c#.

Well I did write a lot but there were some good examples to learn. Ramy already posted the mock class to implement IComparable and since he was using an OleDb* connection you don't know how much data you are dealing with so I chose Dictionary<T,T> to be on the safe side.

I guess I need something better to do on sundays...

hi.. i am trying very hard to understand ur codes as i am not very good in C#. may i ask what does buildsqlnative do and if i would want to search words by a textbox? why do i include this code?

Pretty good explanation & code from scott in post #10 and OP is still clueless123.
>what does buildsqlnative do?
It return a connection string.
>if i would want to search words by a textbox? why do i include this code?
Modify the select query statement.

hi.. i am trying very hard to understand ur codes as i am not very good in C#. may i ask what does buildsqlnative do and if i would want to search words by a textbox? why do i include this code?

see scott, why would a beginner understand Icomparable stuff :)
keep it as simple as possible but not simpler, good proverb huh ? :)

This question has already been answered. Start a new discussion instead.