public void HandleColumnClicked(System.Windows.Forms.ListView listView, ColumnClickEventArgs e)
        {
            // Determine if clicked column is already the column that is being sorted.
            if (e.Column == this.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (this.Order == SortOrder.Ascending)
                {
                    this.Order = SortOrder.Descending;
                }
                else
                {
                    this.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                this.SortColumn = e.Column;
                this.Order = SortOrder.Ascending;
            }

            // Perform the sort with these new sort options.
            listView.Sort();            
        }
Example #2
0
        static void QuickSorts()
        {
            var warmup = new[] { 30, 2, 18, 20, 5, 36, 19 };

            warmup.Sort(x => x, SortingAlgorithm.SelectionSort);
            warmup.Sort(x => x, SortingAlgorithm.InsertionSort);
            warmup.Sort(x => x, SortingAlgorithm.BubbleSort);
            warmup.Sort(x => x, SortingAlgorithm.BubbleSortOptimized);
            warmup.Sort(x => x, SortingAlgorithm.QuickSort);

            var bigCollection = new int[10000000];
            for (var i = 0; i < bigCollection.Length; i++)
            {
                bigCollection[i] = random.Next(int.MinValue, int.MaxValue);
            }

            GC.Collect();
            var watch = new Stopwatch();
            IEnumerable<int> sorted;

            Console.WriteLine("Quick sort:");
            watch.Start();
            sorted = bigCollection.Sort(x => x, SortingAlgorithm.QuickSort);
            watch.Stop();
            Console.WriteLine(
                "Sorted {0} items, time: {1}",
                bigCollection.Length,
                watch.Elapsed);
            Console.WriteLine("Sort Errors: {0}", SortErrorCount(sorted));

            watch.Reset();
            sorted = null;
            GC.Collect();

            Console.WriteLine("Quick sort (parallel):");
            watch.Start();
            sorted = bigCollection
                .Sort(x => x, SortingAlgorithm.QuickSortParallel);
            watch.Stop();
            Console.WriteLine(
                "Sorted {0} items, time: {1}",
                bigCollection.Length,
                watch.Elapsed);
            Console.WriteLine("Sort Errors: {0}", SortErrorCount(sorted));
        }
Example #3
0
 public static string GetSha1(System.Collections.Generic.List<string> codelist)
 {
     codelist.Sort();
     var combostr = string.Empty;
     for (int i = 0; i < codelist.Count; i++)
     {
         combostr += codelist[i];
     }
     return EncryptToSHA1(combostr);
 }
        public static void SortListViewColumn(System.Windows.Forms.ListView listView, int columnIndex, SortOrder order)
        {
            ListViewSorter sorter;
            if (listView.ListViewItemSorter == null)
            {
                ListView.MakeListViewSortable(listView);
            }            
            sorter = (ListViewSorter)listView.ListViewItemSorter;

            sorter.Order = order;
            sorter.SortColumn = columnIndex;
            listView.Sort();
        }
Example #5
0
        public static string DeterminePossibleKnightPositions(string currentPosition)
        {
            var possiblePosition = new[]{
                (char)Convert.ToUInt16(currentPosition[0] +2) + ((char)Convert.ToUInt16(currentPosition[1] +1)).ToString(),
                (char)Convert.ToUInt16(currentPosition[0] +2) + ((char)Convert.ToUInt16(currentPosition[1] -1)).ToString(),
                (char)Convert.ToUInt16(currentPosition[0] +1) + ((char)Convert.ToUInt16(currentPosition[1] +2)).ToString(),
                (char)Convert.ToUInt16(currentPosition[0] +1) + ((char)Convert.ToUInt16(currentPosition[1] -2)).ToString(),
                (char)Convert.ToUInt16(currentPosition[0] -1) + ((char)Convert.ToUInt16(currentPosition[1] +2)).ToString(),
                (char)Convert.ToUInt16(currentPosition[0] -1) + ((char)Convert.ToUInt16(currentPosition[1] -2)).ToString(),
                (char)Convert.ToUInt16(currentPosition[0] -2) + ((char)Convert.ToUInt16(currentPosition[1] +1)).ToString(),
                (char)Convert.ToUInt16(currentPosition[0] -2) + ((char)Convert.ToUInt16(currentPosition[1] -1)).ToString(),
            }.ToList();

            possiblePosition.Sort();

            var knightPositions = possiblePosition
                .Where(pp => pp[0] >= 'a' && pp[0] <= 'h' && pp[1] <= '8' && pp[1] >= '1')
                .Aggregate(string.Empty, (current, pp) => current + (pp + " "));

            return knightPositions.Substring(0,knightPositions.Length-1);
        }
		public void Sort_Comparer_Null ()
		{
			IEnumerable<int> e = new[]{1};
			IComparer<int> c = null;
			e.Sort (c);
		}
Example #7
0
        private void UpdateTab(TabPage tabPage)
        {
            if (tabPage == tabPageEditor && comboBoxFontFile.Items.Count == 0)
            {
                LoadFonts();
            }
            else if (tabPage == tabPageAdvanced && tabPageAdvanced.Controls.Count == 0)
                CreateAdvancedSettings(tabPage);
            else if (tabPage == tabPageEmulator)
            {
                if (comboBoxCommandsKeys.Items.Count == 0)
                {
                    var keys = Enum.GetNames(typeof (Keys)).ToList();
                    keys.Sort();

                    foreach (var k in keys.Where(k => k != "NoName" && k != "None"))
                        comboBoxCommandsKeys.Items.Add(k);
                    ClearCombobox(comboBoxCommandsKeys);

                    var r = new[]
                    {
                        "Show", "Focus", "CopySelection", "Selection", "CopyText", "Text", "Paste", "Alert:<Text>",
                        "Question:<Text>", "Nop", "Wait", "ProgramName", "RandomNumber", "RandomChar", "RandomCHAR",
                    }.ToList();
                    r.Sort();
                    foreach (var k in r)
                        comboBoxCommandsSpecial.Items.Add("{" + k + "}");
                    ClearCombobox(comboBoxCommandsSpecial);

                    textBoxCommands.SelectionStart = textBoxCommands.TextLength;
                    textBoxCommands.SelectionLength = 0;
                }
                textBoxCommands.Select();
                textBoxCommands.ScrollToCaret();
            }
        }
Example #8
0
        public void Matrix_Sort_Rows_Test()
        {
            Matrix m1 = new[,]
            {
                { 1, 3, 6, 5 }, // 0
                { 3, 6, 4, 4 }, // 2
                { 4, 7, 5, 3 }, // 3
                { 2, 9, 3, 2 }, // 1
                { 4, 8, 8, 8 }, // 4
            };

            Vector v = new Vector(new double[] { 0, 3, 1, 2, 4 });

            Vector indices;

            Matrix m1s = m1.Sort(k => k[0], VectorType.Row, true, out indices);

            Assert.Equal(m1[0, 0], m1s[0, 0]);
            Assert.Equal(m1[3, 0], m1s[1, 0]);
            Assert.Equal(m1[1, 0], m1s[2, 0]);
            Assert.Equal(m1[2, 0], m1s[3, 0]);
            Assert.Equal(m1[4, 0], m1s[4, 0]);

            Assert.Equal(m1[0, 1], m1s[0, 1]);
            Assert.Equal(m1[3, 1], m1s[1, 1]);
            Assert.Equal(m1[1, 1], m1s[2, 1]);
            Assert.Equal(m1[2, 1], m1s[3, 1]);
            Assert.Equal(m1[4, 1], m1s[4, 1]);

            Assert.Equal(v[0], indices[0]);
            Assert.Equal(v[1], indices[1]);
            Assert.Equal(v[2], indices[2]);
            Assert.Equal(v[3], indices[3]);
            Assert.Equal(v[4], indices[4]);
        }
		public void Sort_Comparison_Null ()
		{
			IEnumerable<int> e = new[]{1};
			Comparison<int> c = null;
			e.Sort (c);
		}
Example #10
0
        /// <summary>
        /// Calculate Min Max date from a List of dates for Polar chart
        /// </summary>
        /// <param name="dateTimeList">List of DateTimes</param>
        /// <param name="minDate">min Date</param>
        /// <param name="maxDate">out Max Date</param>
        /// <param name="minDateRange">Minimum date difference</param>
        /// <param name="maxDateRange">Maximum date difference</param>
        internal static void CalculateMinMaxDate4PolarChart(System.Collections.Generic.List<DateTime> dateTimeList, Axis axisX, out DateTime minDate, out DateTime maxDate, out TimeSpan minDateRange, out TimeSpan maxDateRange)
        {
            if(axisX.AxisMinimum == null)
                minDate = DateTime.Parse("12/30/1899", System.Globalization.CultureInfo.InvariantCulture);
            else
                minDate = Convert.ToDateTime(axisX.AxisMinimum);

            if (axisX.AxisMaximum != null)
                maxDate = Convert.ToDateTime(axisX.AxisMaximum);
            else
                maxDate = dateTimeList[dateTimeList.Count - 1];

            dateTimeList.Sort();

            minDateRange = new TimeSpan();
            maxDateRange = new TimeSpan();

            if (dateTimeList.Count > 0)
            {
                if (dateTimeList.Count >= 2)
                {
                    minDateRange = dateTimeList[1].Subtract(minDate);
                    maxDateRange = maxDate.Subtract(minDate);
                }
            }
            else
                maxDate = minDate;
        }
Example #11
0
        public void Matrix_Sort_Columns_Test()
        {
            Vector v = new Vector(new double[] { 1, 3, 2, 0 });

            Matrix m1 = new[,]
            {
               // 3  0  2  1
                { 4, 1, 3, 2 },
                { 1, 2, 3, 4 },
                { 7, 9, 8, 6 },
                { 6, 9, 3, 2 },
                { 6, 8, 8, 8 },
            };

            Vector indices;
            Matrix m1s = m1.Sort(k => k[0], VectorType.Col, true, out indices);

            Assert.Equal(m1[0, 3], m1s[0, 1]);
            Assert.Equal(m1[0, 0], m1s[0, 3]);
            Assert.Equal(m1[0, 2], m1s[0, 2]);
            Assert.Equal(m1[0, 1], m1s[0, 0]);

            Assert.Equal(m1[1, 3], m1s[1, 1]);
            Assert.Equal(m1[1, 0], m1s[1, 3]);
            Assert.Equal(m1[1, 2], m1s[1, 2]);
            Assert.Equal(m1[1, 1], m1s[1, 0]);

            Assert.Equal(v[0], indices[0]);
            Assert.Equal(v[1], indices[1]);
            Assert.Equal(v[2], indices[2]);
            Assert.Equal(v[3], indices[3]);
        }
Example #12
0
        public static string CreateIntervalStringFromArray(ref System.Collections.ArrayList a_array)
        {
            bool bAllowExceptClause = false;

            string sResult = "";
            string sExclude = "";
            int nCount = a_array.Count;
            if (nCount == 0)
                return sResult;

            a_array.Sort();
            int nLast = Convert.ToInt32(a_array[0]);
            sResult = nLast.ToString();
            int nThis;
            int nFirstInCurrentRange = nLast;

            for (int i = 1; i < nCount; i++)
            {
                nThis = Convert.ToInt32(a_array[i]);
                if (nThis == nLast) //ignore if a number occurs more than once //TODO: build another list for those?
                    continue;

                if (nThis != nLast+1)
                {
                    if (bAllowExceptClause && nThis == nLast+2)
                    {
                        //TODO: if it's just one that's skipped, continue this range and plce the skipped in the exclude list
                        //ex: 1,2,3,4,5,7,8,9,10,12,13,14,16 can be 1-16 EXCEPT 6,11,15 instead of 1-5,7-10,12-14,16
                        sExclude+=(nLast+1).ToString() + ",";
                    }
                    else
                    {
                        if (nLast != nFirstInCurrentRange) //it was a range (more than one number)
                            sResult+= "-" + nLast.ToString();
                        sResult+=","+nThis.ToString();
                        nFirstInCurrentRange = nThis;
                    }
                }

                nLast = nThis;
            }
            //remove the last ","
            //sResult = sResult.Remove(sResult.Length-1,1);

            //TODO: misses last range end, no..?

            if (bAllowExceptClause && sExclude.Length > 0)
                sResult+=" EXCEPT "+sExclude.Remove(sExclude.Length-1, 1);
            return sResult;
        }
Example #13
0
 public override void Sort(System.Collections.ArrayList list)
 {
     list.Sort();
     Console.WriteLine("QuickSorted  list");
 }
Example #14
0
        /// <summary>
        /// A cleanup method that ensures the XValues are sorted accordingly and set explicitly.  
        /// It will also create the totals for the DUMMY series.
        /// </summary>
        private void AdjustXValues(System.Web.UI.DataVisualization.Charting.Series series)
        {
            bool AddDummyPoints = true;

            if(series.Name == "DUMMY")
                return;
            else if(ChartObj.Series["DUMMY"].Points.Count > 0)
                AddDummyPoints = false;

            // sort the series
            series.Sort(PointSortOrder.Ascending, "X");

            bool IsIndexed = false;

            if(series.IsXValueIndexed)
                IsIndexed = true;
            else
            {
                bool IsFirstPoint = true;
                bool IsLastPointZero = false;

                // the series X values must be set and greater than zero
                foreach(DataPoint pt in series.Points)
                {
                    if(pt.XValue == 0 && !IsFirstPoint && IsLastPointZero)
                    {
                        IsIndexed = true;
                        break;
                    }
                    else if (pt.XValue == 0 && IsFirstPoint)
                        IsLastPointZero = true;

                    IsFirstPoint = false;
                }
            }

            if(IsIndexed)
            {
                series.IsXValueIndexed = false;
                int XValue = 0;

                foreach(DataPoint pt in series.Points)
                    pt.XValue = ++XValue;
            }

            series.Points.AddXY(series.Points[series.Points.Count-1].XValue + 1, 0);
            series.Points[series.Points.Count-1].AxisLabel = "Total";
            series.Points[series.Points.Count-1].Color = Color.Transparent;
            series.Points[series.Points.Count-1].BorderColor = Color.Transparent;

            int index = 0;
            foreach(DataPoint pt in series.Points)
            {
                if(AddDummyPoints)
                {
                    ChartObj.Series["DUMMY"].Points.AddXY(pt.XValue, pt.YValues[0]);
                }
                else
                {
                    ChartObj.Series["DUMMY"].Points[index].YValues[0] += pt.YValues[0];
                }

                index++;
            }
        }
Example #15
0
        public void QuickSortTest()
        {
            //Arrange
            var quickSort = new QuickSort<int>();

            var arrayToSort = new []{ 19, 42, 25, 17, 10, 73, 13, 88, 80, 91, 18, 50 };
            //_arrayToSort.CopyTo(arrayToSort, 0);
            //Act
            arrayToSort.Sort(quickSort);
            //Assert
            Assert.IsTrue(arrayToSort.IsSorted());
        }
        public async Task Replace_in_sorted_from_ListChangedNotification_observable()
        {
            var observable = new[]
                {
                    new ListChangedNotification<int>(ImmutableList<int>.Empty, NotifyCollectionChangedAction.Reset, ImmutableList<int>.Empty, ImmutableList<int>.Empty, null),
                    new ListChangedNotification<int>(ImmutableList.Create(1, 2), NotifyCollectionChangedAction.Add, ImmutableList<int>.Empty, ImmutableList.Create(1, 2), 0),
                    new ListChangedNotification<int>(ImmutableList.Create(3, 4), NotifyCollectionChangedAction.Replace, ImmutableList.Create(1, 2), ImmutableList.Create(3, 4), 0)
                }
                .ToObservable()
                .ToReactiveCollection();

            var projectedList = observable.Sort();

            var notificationsTask = projectedList.Changes
                .Skip(2)
                .Take(2)
                .ToArray()
                .ToTask();

            var notifications = await notificationsTask;

            notifications[0].Action.Should().Be(NotifyCollectionChangedAction.Reset);
            notifications[1].Index.Should().Be(0);
            notifications[1].OldItems.Should().BeEmpty();
            notifications[1].NewItems.Should().Equal(3, 4);
            notifications[1].Action.Should().Be(NotifyCollectionChangedAction.Add);
            notifications[1].Current.Should().Equal(3, 4);
        }
Example #17
0
 public void ThreeWayQuickSortTest()
 {
     //Arrange
     var threeWayQuickSort = new ThreeWayQuickSort<int>();
     var arrayToSort = new []{ 53, 14, 39, 96, 37, 27, 53, 73, 53, 53};
     arrayToSort.CopyTo(arrayToSort, 0);
     //Act
     arrayToSort.Sort(threeWayQuickSort);
     //Assert
     Assert.IsTrue(arrayToSort.IsSorted());
 }
Example #18
0
 private bool SortArray(System.Collections.ArrayList vettore)
 {
     bool result = false;
     vettore.Sort();
     result = true;
     return result;
 }
Example #19
0
 public void SortWithCompareCallbackWorks()
 {
     var arr = new[] { 1, 6, 6, 4, 2 };
     arr.Sort((x, y) => (int)y - (int)x);
     Assert.AreEqual(arr, new[] { 6, 6, 4, 2, 1 });
 }
Example #20
0
        /// <summary>
        /// Calculate Min Max date from a List of dates
        /// </summary>
        /// <param name="dateTimeList">List of DateTimes</param>
        /// <param name="minDate">out min Date</param>
        /// <param name="maxDate">out Max Date</param>
        /// <param name="minDateRange">Minimum date difference</param>
        /// <param name="maxDateRange">Maximum date difference</param>
        public static void CalculateMinMaxDate(System.Collections.Generic.List<DateTime> dateTimeList, out DateTime minDate, out DateTime maxDate, out TimeSpan minDateRange, out TimeSpan maxDateRange)
        {   
            dateTimeList.Sort();

            minDateRange = new TimeSpan();
            maxDateRange = new TimeSpan();

            minDate = dateTimeList[0];

            if (dateTimeList.Count > 0)
            {
                maxDate = dateTimeList[dateTimeList.Count - 1];

                if (dateTimeList.Count >= 2)
                {
                    minDateRange = dateTimeList[1].Subtract(minDate);
                    maxDateRange = maxDate.Subtract(minDate);
                }
            }
            else
                maxDate = minDate;
        }
Example #21
0
 public void SortWithDefaultCompareWorks()
 {
     var arr = new[] { 1, 6, 6, 4, 2 };
     arr.Sort();
     Assert.AreEqual(arr, new[] { 1, 2, 4, 6, 6 });
 }
        private static void SetListViewSorter(System.Windows.Forms.ListView listview, int col)
        {
            if (listview.InvokeRequired)
            {
                SetListViewSorterCallBack d = new SetListViewSorterCallBack(SetListViewSorter);
                listview.Invoke(d, new object[] { listview, col });
            }
            else
            {
                ListViewItemComparer comparer = (ListViewItemComparer)listview.ListViewItemSorter;
                comparer.SetColumn(col);
                //選択されているColに昇順・降順ボタン
                int cnt = 0;
                foreach (System.Windows.Forms.ColumnHeader citem in listview.Columns)
                {
                    string str = citem.Text;
                    str = str.Replace("▼", "").Replace("▲", "");
                    //選択されている列
                    if (cnt == col)
                    {
                        if (comparer.Order == SortOrder.Descending) str = "▼" + str;
                        else if (comparer.Order == SortOrder.Ascending) str = "▲" + str;
                    }
                    //名前のセット
                    citem.Text = str;
                    cnt++;
                }

                listview.Sort();
            }
        }