private void DataGridServices_Sorting(object sender, DataGridSortingEventArgs e) { IComparer comparer; if (e == null) { comparer = new ResultSort(ListSortDirection.Ascending, "LikeTerminal"); (ServicesView as ListCollectionView).CustomSort = comparer; return; } DataGridColumn column = e.Column; //i do some custom checking based on column to get the right comparer //i have different comparers for different columns. I also handle the sort direction //in my comparer // prevent the built-in sort from sorting e.Handled = true; ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending; //set the sort order on the column column.SortDirection = direction; //use a ListCollectionView to do the sort. //ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(this.ItemsSource); //this is my custom sorter it just derives from IComparer and has a few properties //you could just apply the comparer but i needed to do a few extra bits and pieces comparer = new ResultSort(direction, column.Header as string); //apply the sort (ServicesView as ListCollectionView).CustomSort = comparer; }
internal void ApplyDefaultSort(IEnumerable <Document> documents, ResultSort oSort) { Field oField; if (oSort.Order) { documents = oResultDocuments.OrderBy(oDocument => (oField = oDocument.GetField(oSort.Field)) != null ? oField.StringValue : string.Empty); } else { documents = oResultDocuments.OrderByDescending(oDocument => (oField = oDocument.GetField(oSort.Field)) != null ? oField.StringValue : string.Empty); } oResultDocuments = documents.ToArray(); }
protected void ApplySort(ResultSort oSort) { IEnumerable <Document> oDocuments; Field oField; if (oSort.Order) { oDocuments = ResultDocuments.OrderBy(oDocument => (oField = oDocument.GetField(oSort.Field)) != null ? oField.StringValue : string.Empty); } else { oDocuments = ResultDocuments.OrderByDescending(oDocument => (oField = oDocument.GetField(oSort.Field)) != null ? oField.StringValue : string.Empty); } oResultDocuments = oDocuments.ToArray(); }
// Optional: sort (default is back_no) public List<ResultItem> GetResultItemList(ResultSort sort = ResultSort.Default) { // Case statment for sort column string sortString = null; switch (sort) { case ResultSort.BackNo: sortString = "b.back_no, s.date, c.class_no"; break; case ResultSort.Class: sortString = "c.class_no"; break; case ResultSort.Horse: sortString = "h.horse_no"; break; case ResultSort.Place: sortString = "t.place"; break; case ResultSort.Rider: sortString = "r.rider_last, r.rider_first, s.date, c.class_no, t.place"; break; case ResultSort.Show: sortString = "s.date, c.class_no"; break; default: sortString = "back_no"; break; } string query = "SELECT b.back_no, r.rider_no, r.rider_first, r.rider_last, h.horse_no, h.horse_name, " + "s.show_no, s.date, c.class_no, c.class_name, t.place, t.time, t.points, t.paid_in, t.paid_out " + "FROM [" + Year + "_result] AS t JOIN [" + Year + "_backNo] AS b ON t.back_no = b.back_no " + "JOIN [" + Year + "_rider] AS r ON b.rider_no = r.rider_no " + "JOIN [" + Year + "_horse] AS h ON b.horse_no = h.horse_no " + "JOIN [" + Year + "_class] AS c ON t.class_no = c.class_no " + "JOIN [" + Year + "_show] AS s ON t.show_no = s.show_no " + "ORDER BY " + sort + ";"; SQLiteDataReader reader = DoTheReader(ClubConn, query); List<ResultItem> resultItemList = new List<ResultItem>(); ResultItem item; reader = DoTheReader(ClubConn, query); while (reader.Read()) { item = new ResultItem(); item.BackNo = reader.GetInt32(0); item.RiderNo = reader.GetInt32(1); item.Rider = reader.GetString(2) + " " + reader.GetString(3); item.HorseNo = reader.GetInt32(4); item.Horse = reader.GetString(5); item.ShowNo = reader.GetInt32(6); item.ShowDate = reader.GetString(7); item.ClassNo = reader.GetInt32(8); item.ClassName = reader.GetString(9); item.Place = reader.GetInt32(10); item.Time = reader.GetDecimal(11); item.Points = reader.GetInt32(12); item.PayIn = reader.GetDecimal(13); item.PayOut = reader.GetDecimal(14); resultItemList.Add(item); } reader.Close(); ClubConn.Close(); return resultItemList; }
static void Main(string[] args) { var musicDir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)); ResultSort resultSort = ResultSort.ByAlbumCount; foreach (var arg in args) { if (arg.Trim().StartsWith("alpha", StringComparison.CurrentCultureIgnoreCase)) { resultSort = ResultSort.ByArtistName; } else { try { if (Path.GetFullPath(arg) != "") { musicDir = new DirectoryInfo(arg); } } catch (Exception) { Console.WriteLine("Invalid path specified: {0}", arg); return; } } } if (musicDir.Exists) { var artistAlbums = new Dictionary <String, int>(); var artistDirs = musicDir.GetDirectories(); var longestArtistName = 0; foreach (var artistDir in artistDirs) { artistAlbums.Add(artistDir.Name, artistDir.EnumerateDirectories().Count()); if (artistDir.Name.Length > longestArtistName) { longestArtistName = artistDir.Name.Length; } } if (artistAlbums.Count == 0) { Console.WriteLine("No artist directories found in {0}", musicDir.FullName); } else { foreach (var aa in resultSort == ResultSort.ByAlbumCount ? artistAlbums.OrderByDescending(aa => aa.Value).ThenBy(aa => aa.Key) : artistAlbums.OrderBy(aa => aa.Key)) { Console.WriteLine("{0,-" + (longestArtistName) + "}{1,10}", aa.Key, aa.Value); } Console.WriteLine("{0,-" + (longestArtistName) + "}{1,10}", "TOTAL ALBUMS:", artistAlbums.Count); } } else { Console.WriteLine("Folder '{0}' not found / acccessible", musicDir.FullName); } }