I implemented a search function like this:private static void GetResults(ref ObservableCollection<string> resultTitles, ref string[] query, ref ObservableCollection<int> weight){    int position = -1;    foreach (string[] r in SearchKeys.Keys)    {        position++;        foreach (string s in query)        {            int m = r.Length/2;            int min = r[m][0] < s[0] ? m : 0;            int max = r[m][0] <= s[0] ? r.Length : m+1;            for (int i = min; i < max; i++)            {                weight.Add(0);                if (r[i] == s)                {                    if (weight[position] == 0)                    {                        resultTitles.Add(r[0]);                    }                    weight[position]++;                }   // end if            }   // end foreach(s)        }   // end foreach(t)    }   // end foreach(r)}In the main program, the use inputs a query, the program splits the query at the spaces, and passes the query and two empty ObservableCollections to this function.SearchKeys.Keys is implemented like this:public static string[][] Keys = { Array1, Array2, Array3 };The arrays are implemented like this:private static string[] Array1 = { Title, val1, val2, val3, val4, val5, val6 };As always, all comments are welcome; particularly, I am interested in which ways could this code's performance be improved.  The data in SearchKeys is sorted, of course.This is a previous version of my code, which may also be of interest.  This version should run about twice as slow as the above, according to my estimates:private void getResults(ref ObservableCollection<string> tmp, ref string[] query, ref ObservableCollection<int> weight){    int position = -1;    foreach (string[] r in SearchKeys.Keys)    {        position++;        foreach (string t in r)        {            weight.Add(0);            foreach (string s in query)            {                if (t == s)                {                    if (weight[position] == 0)                    {                        tmp.Add(r[0]);                    }                    weight[position]++;                }   // end if            }   // end foreach(s)        }   // end foreach(t)    }   // end foreach(r)}This is how I call this function:static void Main(string[] args){    ObservableCollection<int> weight = new ObservableCollection<int>();    ObservableCollection<string> resultTitles = new ObservableCollection<string>();    string[] query = Console.ReadLine().Split(' ');    GetResults(ref resultTitles, ref query, ref weight);    StableSort(ref resultTitles, ref weight);    foreach (string s in resultTitles)        Console.WriteLine(resultTitles.IndexOf(s)+: +s);}This is StableSort (see Stable Sort in C#):static void StableSort(ref ObservableCollection<string> values, ref ObservableCollection<int> weights){    while(weights.Contains(0))    {        weights.Remove(0);    }    if (values == null) { throw new ArgumentNullException(values); }    if (weights == null) { throw new ArgumentNullException(weights); }    if (values.Count != weights.Count) { throw new ArgumentOutOfRangeException(collections count not equal, (Exception)null); }    ObservableCollection<string> localValues = new ObservableCollection<string>();    ObservableCollection<int> localWeights = new ObservableCollection<int>();    int index = -1;    var weightsWithIndex = weights.Select(p => new { Value = p, Index = ++index }).OrderByDescending(p => p.Value);    foreach (var w in weightsWithIndex)    {        localWeights.Add(w.Value);        localValues.Add(values[w.Index]);    }    values = localValues;    weights = localWeights;}
Search arrays for values
c#;search
Focusing on  foreach (string[] r in SearchKeys.Keys){    position++;    foreach (string s in query)    {        int m = r.Length/2;        int min = r[m][0] < s[0] ? m : 0;        int max = r[m][0] <= s[0] ? r.Length : m+1;  r.Length won't change by iterating over query nor does r[m][0] foreach (string[] r in SearchKeys.Keys){    position++;    int length = r.Length;    int middleOfArray = length / 2;    Char firstKeyItemCharacter = r[middleOfArray][0];    foreach (string s in query)    {        int min = firstKeyItemCharacter < s[0] ? middleOfArray : 0;        int max = firstKeyItemCharacter <= s[0] ? length : middleOfArray + 1;instead of adding to weight for each iteration of the most inner loop, add it at the most outer loop.  Because you remove all entries of weight where the entry == 0 in the StableSort() method, we can do this here using the Where() method. Btw. by first removing the 0 entries the check for weights == null will be senseless.  private static void GetResults(ref ObservableCollection<string> resultTitles, ref string[] query, ref ObservableCollection<int> weight){    int position = -1;    IList<int> currentWeights = new List<int>();    foreach (string[] r in SearchKeys.Keys)    {        currentWeights.Add(0);        position++;        int length = r.Length;        int middleOfArray = length / 2;        Char firstKeyItemCharacter = r[middleOfArray][0];        foreach (string s in query)        {            int min = firstKeyItemCharacter < s[0] ? middleOfArray : 0;            int max = firstKeyItemCharacter <= s[0] ? length : middleOfArray + 1;            for (int i = min; i < max; i++)            {                if (r[i] == s)                {                    if (currentWeights[position] == 0)                    {                        resultTitles.Add(r[0]);                    }                    currentWeights[position]++;                }            }        }    }    weight = new ObservableCollection<int>(currentWeights.Where(x => x > 0));}