Commits

Anonymous committed aea25cc

enabling lame process sorting by PID or Name (no you cannot do reverse sorting)

  • Participants
  • Parent commits e4632dc

Comments (0)

Files changed (2)

File FormMain.Designer.cs

             this.listViewProcesses.UseCompatibleStateImageBehavior = false;
             this.listViewProcesses.View = System.Windows.Forms.View.Details;
             this.listViewProcesses.SelectedIndexChanged += new System.EventHandler(this.listViewProcesses_SelectedIndexChanged);
+            this.listViewProcesses.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.onListViewProcessesColumnClick);
             this.listViewProcesses.DoubleClick += new System.EventHandler(this.listViewProcesses_DoubleClick);
             // 
             // columnHeaderProcessId
                 this.DisplayInstanceDetails(newInstance);
             }
         }
+
+        private void onListViewProcessesColumnClick(object sender, ColumnClickEventArgs e)
+        {
+            sortListViewOnColumnIndex(sender as ListView, e.Column);
+        }
+
+        private void sortListViewOnColumnIndex(ListView listView, int columnIndex)
+        {
+            if(isColumnInteger(listView,columnIndex))
+            {
+                sortListViewAsIntegers(listView, columnIndex);
+            }
+            else
+            {
+                sortListViewAsStrings(listView, columnIndex);
+            }
+        }
+
+        private void sortListViewAsStrings(ListView listView, int columnIndex)
+        {
+            var newitems = listView.Items.OfType<ListViewItem>().OrderBy(item => item.SubItems[columnIndex].Text).ToArray();
+            swapItems(listView, newitems);
+        }
+
+        private void sortListViewAsIntegers(ListView listView, int columnIndex)
+        {
+            var newitems  = listView.Items.OfType<ListViewItem>().OrderBy(item => Int32.Parse(item.SubItems[columnIndex].Text)).ToArray();
+            swapItems(listView, newitems);
+        }
+
+        private void swapItems(ListView listView, ListViewItem[] newitems)
+        {
+            // http://msdn.microsoft.com/en-us/library/ms996467.aspx HA!
+            listView.Items.Clear();
+            listView.Items.AddRange(newitems);
+        }
+
+        private bool isColumnInteger(ListView listView, int columnIndex)
+        {
+            if(listView == listViewProcesses)
+            {
+                return columnIndex == 0;
+            }
+            return false;
+        }
     }
 }