Example #1
0
        public static ArrayList ListSort(
            [DekiScriptParam("list of values")] ArrayList list,
            [DekiScriptParam("key for value if list contains maps (default: nil)", true)] object key,
            [DekiScriptParam("sort in reverse order (default: false)", true)] bool? reverse,
            [DekiScriptParam("compare two items (return -1 if left item less than the right item, 0 if they are equal, and +1 if the left item is greater than the right item; use '$left' and '$right' to refer to the left and right items respectively)", true)] string compare,
            DekiScriptRuntime runtime
       ) {

            // prepare custom comparer
            IComparer comparer = null;
            if(compare != null) {
                comparer = new DekiScriptComparer(runtime, DekiScriptParser.Parse(new Location("list.sort(compare)"), compare));
            }

            // sort list
            if(key == null || (key as string != null && string.IsNullOrEmpty(key as string))) {
                list.Sort(comparer);
            } else {
                Array keys = (key is ArrayList) ? ((ArrayList)key).ToArray() : ListCollect(list, key.ToString(), runtime).ToArray();
                Array values = list.ToArray();
                Array.Sort(keys, values, comparer);
                list = new ArrayList(values);
            }

            // check if results need to be reveresed
            if(reverse ?? false) {
                list.Reverse();
            }
            return list;
        }
Example #2
0
        public static ArrayList ListSort(
            [DekiScriptParam("list of values")] ArrayList list,
            [DekiScriptParam("key for value if list contains maps (default: nil)", true)] object key,
            [DekiScriptParam("sort in reverse order (default: false)", true)] bool?reverse,
            [DekiScriptParam("compare two items (return -1 if left item less than the right item, 0 if they are equal, and +1 if the left item is greater than the right item; use '$left' and '$right' to refer to the left and right items respectively)", true)] string compare,
            DekiScriptRuntime runtime
            )
        {
            // prepare custom comparer
            IComparer comparer = null;

            if (compare != null)
            {
                comparer = new DekiScriptComparer(runtime, DekiScriptParser.Parse(new Location("list.sort(compare)"), compare));
            }

            // sort list
            if (key == null || (key as string != null && string.IsNullOrEmpty(key as string)))
            {
                list.Sort(comparer);
            }
            else
            {
                Array keys   = (key is ArrayList) ? ((ArrayList)key).ToArray() : ListCollect(list, key.ToString(), runtime).ToArray();
                Array values = list.ToArray();
                Array.Sort(keys, values, comparer);
                list = new ArrayList(values);
            }

            // check if results need to be reveresed
            if (reverse ?? false)
            {
                list.Reverse();
            }
            return(list);
        }