예제 #1
0
        private void SortList()
        {
            Functions.Sort((f1, f2) =>
            {
                // The first function in the list should be the most specialized.
                // If the execute the command we will iterate through the list from the beginning
                // and choose the first matching function.

                // Sort out special arguments
                // and remove the nullable wrapper
                var params1 = (from p in f1.CommandParameter
                               where p.kind.IsNormal()
                               select FunctionCommand.UnwrapType(p.type)).ToList();

                var params2 = (from p in f2.CommandParameter
                               where p.kind.IsNormal()
                               select FunctionCommand.UnwrapType(p.type)).ToList();

                for (int i = 0; i < params1.Count; i++)
                {
                    // Prefer functions with higher parameter count
                    if (i >= params2.Count)
                    {
                        return(-1);
                    }
                    int i1 = Array.IndexOf(XCommandSystem.TypeOrder, params1[i]);
                    if (i1 == -1)
                    {
                        i1 = XCommandSystem.TypeOrder.Length;
                    }
                    int i2 = Array.IndexOf(XCommandSystem.TypeOrder, params2[i]);
                    if (i2 == -1)
                    {
                        i2 = XCommandSystem.TypeOrder.Length;
                    }
                    // Prefer lower argument
                    if (i1 < i2)
                    {
                        return(-1);
                    }
                    if (i1 > i2)
                    {
                        return(1);
                    }
                }
                if (params2.Count > params1.Count)
                {
                    return(1);
                }

                return(0);
            });
        }