public async Task <List <Product> > SortProduct(string type) { ISorting sorting; List <Product> productList = await _context.Products .AsNoTracking() .ToListAsync(); switch (type) { case "name": sorting = new SortByName(); sorting.ProductSort(ref productList); break; case "price": sorting = new SortByPrice(); sorting.ProductSort(ref productList); break; case "quantity": sorting = new SortByQuantity(); sorting.ProductSort(ref productList); break; case "rating": sorting = new SortByRating(); sorting.ProductSort(ref productList); break; default: sorting = new SortByPrice(); sorting.ProductSort(ref productList); break; } return(productList); }
private void comboBoxSort_SelectedIndexChanged(object i_Sender, EventArgs i_E) { setComboBoxEnabled(comboBoxSort, false); listBoxCheckins.Invoke(new Action(() => listBoxCheckins.Items.Clear())); pictureBoxCheckIn.Image = null; // Reset Image webBrowserGoogleMaps.Navigate("about:blank"); // reset web browser url switch (comboBoxSort.SelectedItem.ToString()) { case "Date": StrategySorterCheckins = new SortByDate <Checkin>(); break; case "Name": StrategySorterCheckins = new SortByName <Checkin>(); break; case "Likes": MessageBox.Show(@"Feature isnt available due to facebook limits"); StrategySorterCheckins = null; // because feature isnt supported by the dll (but it will work if it does) // StrategySorterCheckins = new SortByLikes<Checkin>(); break; default: MessageBox.Show(@"Something went wrong, try again"); StrategySorterCheckins = null; break; } if (StrategySorterCheckins != null) { insertToListBox(CheckinsData.SortStrategy( m_FilterdCheckinList.ObjectList, StrategySorterCheckins)); } }
/// <summary> /// Sort the contents of a TreeView alphabetically within each group Node. /// </summary> /// <param name="treTree">TreeView to sort.</param> public void SortCustom() { for (int i = 0; i <= this.Nodes.Count - 1; i++) { List<TreeNode> lstNodes = new List<TreeNode>(); foreach (TreeNode objNode in this.Nodes[i].Nodes) lstNodes.Add(objNode); this.Nodes[i].Nodes.Clear(); try { SortByName objSort = new SortByName(); lstNodes.Sort(objSort.Compare); } catch { } foreach (TreeNode objNode in lstNodes) this.Nodes[i].Nodes.Add(objNode); this.Nodes[i].Expand(); } }
public void orderList() { if (Session["selectedcourse"] != null && !string.IsNullOrEmpty(Session["selectedcourse"].ToString())) { using (var context = new StudentRecordEntities1()) { string s = Session["selectedcourse"].ToString(); Course course = context.Courses.Where(c => c.Code.Equals(s)).FirstOrDefault(); if (course == null) { selectCourseError.Text = "Please select a course"; return; } TableRow header = tblStudents.Rows[0]; tblStudents.Rows.Clear(); tblStudents.Rows.Add(header); List <AcademicRecord> list = course.AcademicRecords.ToList(); if (Session["sort"] != null && (string)Session["sort"] == Request.Params["sort"]) { if (Session["order"] != null && Session["order"].ToString() == "ascending") { this.order = "descending"; Session["order"] = this.order; } else { this.order = "ascending"; Session["order"] = this.order; } } else { this.order = "ascending"; Session["order"] = this.order; } Session["sort"] = Request.Params["sort"]; IComparer <AcademicRecord> comparer; switch (Request.Params["sort"]) { case "id": comparer = new SortById(this.order); list.Sort(comparer); break; case "name": comparer = new SortByName(this.order); list.Sort(comparer); break; case "grade": comparer = new SortByGrade(this.order); list.Sort(comparer); break; } foreach (AcademicRecord student in list) { TableRow row = new TableRow(); TableCell cell = new TableCell(); cell.Text = student.Student.Id; row.Cells.Add(cell); cell = new TableCell(); cell.Text = student.Student.Name; row.Cells.Add(cell); cell = new TableCell(); cell.Text = student.Grade + ""; row.Cells.Add(cell); tblStudents.Rows.Add(row); } } } }
public static void Main(string[] args) { //________________________________________ Students Info___________________________________________________ Student student0 = new Student() { Name = "", LastName = "", Hometown = "", Food = "", FavoriteBand = "", FavoriteColor = "" }; Student student1 = new Student() { Name = "Dakota", LastName = "Slabbekorn", Hometown = "Kent City", Food = "Pho", FavoriteBand = "Trap", FavoriteColor = "Red" }; Student student2 = new Student() { Name = "Joshua", LastName = "Bultsma", Hometown = "Grand Rapids", Food = "Cheese Tortellini", FavoriteBand = "Hip Hop", FavoriteColor = "Pink" }; Student student3 = new Student() { Name = "Tommy", LastName = "Waalkes", Hometown = "Releigh", Food = "Chicken Curry", FavoriteBand = "", FavoriteColor = "Not White" }; Student student4 = new Student() { Name = "James", LastName = "Dockery", Hometown = "Grand Rapids", Food = "Cheeseburgers", FavoriteBand = "Rock", FavoriteColor = "Red" }; Student student5 = new Student() { Name = "Maricela", LastName = "Rivera", Hometown = "Morelia Mexico", Food = "Tacos", FavoriteBand = "Country", FavoriteColor = "Blue" }; Student student6 = new Student() { Name = "Kimvy", LastName = "Nguyen", Hometown = "Grand Rapids", Food = "Sushi", FavoriteBand = "Rock", FavoriteColor = "White" }; Student student7 = new Student() { Name = "Sam", LastName = "Thomson", Hometown = "Grand Rapids", Food = "Tacos", FavoriteBand = "nickelback", FavoriteColor = "Black" }; List <Student> students = new List <Student>(24); students.Add(student0); students.Add(student1); students.Add(student2); students.Add(student3); students.Add(student4); students.Add(student5); students.Add(student6); students.Add(student7); Console.WriteLine("Welcom to the Grand Circus Back End developemnt After hour class of 2019"); //________________________________________ Students Search ______________________________________________________ bool searchMore = true; while (searchMore) { int sNum = 0; Console.WriteLine("Type a number from 1-{0} to find out about a student", students.Count - 1); SortByName sortByName = new SortByName(); students.Sort(sortByName); for (int i = 1; i < students.Count; i++) { Student stu = students[i]; Console.WriteLine(i + "-" + stu.Name); } string input1 = Console.ReadLine(); try { sNum = int.Parse(input1); } catch (FormatException) { Console.WriteLine("Invalid entry. you must enter a number"); input1 = Console.ReadLine(); sNum = int.Parse(input1); } while (sNum > students.Count - 1) { Console.WriteLine("you must enter a number btween 1 and {0} ", students.Count - 1); input1 = Console.ReadLine(); try { sNum = int.Parse(input1); } catch (FormatException) { Console.WriteLine("Invalid entry. you must enter a number"); input1 = Console.ReadLine(); sNum = int.Parse(input1); } } //_________________________________________Caracteristics______________________________________________________ bool moreCaracteristics = true; while (moreCaracteristics) { int input2 = 0; Console.WriteLine("Student {0} is {1} {2} What would you like to know about {1}?", sNum, students[sNum].Name, students[sNum].LastName); Console.WriteLine("Choises"); Console.WriteLine("1- Hometown"); Console.WriteLine("2- Favorite Food"); Console.WriteLine("3- Favorite Musician"); Console.WriteLine("4- Favorite Color"); string inp = Console.ReadLine(); try { input2 = int.Parse(inp); } catch (FormatException) { Console.WriteLine("invalid entry. please enter an intiger"); } while (input2 > 4) { Console.WriteLine("Invalid entry. must enter a number between 1 and 4"); inp = Console.ReadLine(); try { input2 = int.Parse(inp); } catch (FormatException) { Console.WriteLine("Invalid entry. must enter a number between 1 and 4"); } } if (input2 == 1) { Console.WriteLine("{0}'s Hometown is: {1} ", students[sNum].Name, students[sNum].Hometown); } if (input2 == 2) { Console.WriteLine("{0}'s Favorite Food is: {1} ", students[sNum].Name, students[sNum].Food); } if (input2 == 3) { Console.WriteLine("{0}'s Favorite Band is: {1} ", students[sNum].Name, students[sNum].FavoriteBand); } if (input2 == 4) { Console.WriteLine("{0}'s Favorite Color is: {1} ", students[sNum].Name, students[sNum].FavoriteColor); } Console.WriteLine("Whould you like to know more about {0}?", students[sNum].Name); string oc = Console.ReadLine().ToLower(); while (oc != "n" && oc != "y") { Console.WriteLine("Invalid entry. Enter n/y"); oc = Console.ReadLine().ToLower(); } if (oc == "n") { moreCaracteristics = false; } } //_________________________________________Add Student__________________________________________________________ bool addMoreStudent = true; while (addMoreStudent) { Console.WriteLine("Would you like to add a student? y/n"); string adding = Console.ReadLine(); if (adding == "y") { Console.WriteLine("What is the First Name of the student"); string nameInput = Console.ReadLine(); Console.WriteLine("What is the Last Name of the student"); string lastNameInput = Console.ReadLine(); Console.WriteLine("What is the Hometown of the Student"); string homeInput = Console.ReadLine(); Console.WriteLine("What is the Favorite Food of the Student"); string foodInput = Console.ReadLine(); Console.WriteLine("What is the Favorite Band of the Student"); string bandInput = Console.ReadLine(); Console.WriteLine("What is the Favorite Color of the Student"); string colorInput = Console.ReadLine(); students.Add(new Student { Name = nameInput, Hometown = homeInput, Food = foodInput, FavoriteBand = bandInput, FavoriteColor = colorInput, LastName = lastNameInput }); Console.WriteLine("Do you want to add another Student?"); string a = Console.ReadLine().ToLower(); while (a != "n" && a != "y") { Console.WriteLine("Invalid entry. It must be n or y"); a = Console.ReadLine().ToLower(); } if (a == "n") { addMoreStudent = false; } } else { addMoreStudent = false; } } Console.WriteLine("List of Students:"); sortByName = new SortByName(); students.Sort(sortByName); for (int i = 1; i < students.Count; i++) { Student stu = students[i]; Console.WriteLine(i + "-" + stu.Name); } Console.WriteLine("Do you want to search info about one of this students?"); string repeat = Console.ReadLine().ToLower(); while (repeat != "n" && repeat != "y") { Console.WriteLine("Invalid entry. It must be n or y"); repeat = Console.ReadLine().ToLower(); } if (repeat == "n") { Console.WriteLine("Good Bye"); searchMore = false; } } }
int IComparable <Person> .CompareTo(Person other) { return(SortByName.Compare(this, other)); }
static void Main() { Customer customer1 = new Customer { Id = 130, Name = "John Green", Salary = 70000 }; Customer customer2 = new Customer { Id = 150, Name = "Chris Green", Salary = 30000 }; Customer customer3 = new Customer { Id = 100, Name = "Mike Green", Salary = 100000 }; List <Customer> listCustomers = new List <Customer>(); listCustomers.Add(customer1); listCustomers.Add(customer2); listCustomers.Add(customer3); listCustomers.Add(customer2); foreach (Customer c in listCustomers) { Console.WriteLine(c.Name); } //Simple Find Method--------------------parameter here--property to check var returnCustomer = listCustomers.Find(customer => customer.Id == 150); Console.WriteLine("Returned : " + returnCustomer.Name); Console.WriteLine("------------------------------"); Console.WriteLine("Salaries before Sort"); foreach (Customer c in listCustomers) { Console.WriteLine(c.Salary); } Console.WriteLine("------------------------------"); listCustomers.Sort(); Console.WriteLine("Salaries after Sort"); foreach (Customer c in listCustomers) { Console.WriteLine(c.Salary); } Console.WriteLine("------------------------------"); listCustomers.Reverse(); Console.WriteLine("Salaries after Sort Reverse"); foreach (Customer c in listCustomers) { Console.WriteLine(c.Salary); } Console.WriteLine("------------------------------"); SortByName sortByName = new SortByName(); listCustomers.Sort(sortByName); Console.WriteLine("Sorted by Name"); foreach (Customer c in listCustomers) { Console.WriteLine(c.Name); } #region //This regions describes the Comparison Delegate the long wan Console.WriteLine("------------------------------"); // Console.WriteLine("Before Sort Id using the Comparison Delegate"); // // Comparison <Customer> customerComparer = new Comparison<Customer>(CompareCustomer); // // foreach(Customer c in listCustomers) // { // Console.WriteLine(c.Id); // } // // listCustomers.Sort(customerComparer); Console.WriteLine("After Sort Id using the Comparison Delegate"); // // foreach (Customer c in listCustomers) // { // Console.WriteLine(c.Id); // } #endregion //This is a shorter way using the delegate keyword for the above Comparison // listCustomers.Sort(delegate (Customer x, Customer y) // { // return x.Id.CompareTo(y.Id); // }); // foreach (Customer c in listCustomers) // { // Console.WriteLine(c.Id); // } //This is an even shorter way using the lambda expression for the above Comparison listCustomers.Sort((x, y) => x.Id.CompareTo(y.Id)); foreach (Customer c in listCustomers) { Console.WriteLine(c.Id); } //Using TrueForAll() Check all elements to see if they meet a condition in the predicate Console.WriteLine($"Are all the employees over 50000 in salary? {listCustomers.TrueForAll(x => x.Salary > 50000)}"); //uses System.Collections.ObjectModel for the return. you cannot just call it on the list customers; ReadOnlyCollection <Customer> readonlyCustomers = listCustomers.AsReadOnly(); //readonlyCustomers.Add(customer1); wont work Console.WriteLine("Items: " + readonlyCustomers.Count); //you can count them though List <Customer> listCapacityCustomers = new List <Customer>(100); listCapacityCustomers.Add(customer1); listCapacityCustomers.Add(customer2); listCapacityCustomers.Add(customer3); Console.WriteLine("------------------------------"); Console.WriteLine("Capacity b4 trimming: " + listCapacityCustomers.Capacity); listCapacityCustomers.TrimExcess(); Console.WriteLine("Capacity After trimming: " + listCapacityCustomers.Capacity); Console.WriteLine("------------------------------"); Console.WriteLine("----------- Queues--------------------------"); //Using the Queue Queue <Customer> queueCustomers = new Queue <Customer>(); queueCustomers.Enqueue(customer1); queueCustomers.Enqueue(customer2); queueCustomers.Enqueue(customer3); queueCustomers.Enqueue(customer2); int cntr = 0; foreach (var cust in queueCustomers) { cntr++; Console.WriteLine($"Listing of Customer no. {cntr} : {cust.Name}"); } Console.WriteLine("------------------------------"); Customer cqp = queueCustomers.Peek(); Console.WriteLine("Customer Name that was peeked: " + cqp.Name); Console.WriteLine("------------------------------"); //Removes from the top of the list Customer dequedCustomer = queueCustomers.Dequeue(); Console.WriteLine("Customer Name removed: " + dequedCustomer.Name); Console.WriteLine("------------------------------"); cntr = 0; foreach (var cust in queueCustomers) { cntr++; Console.WriteLine($"Listing of Customer no. {cntr} : {cust.Name}"); } Console.WriteLine("--------------Stacks----------------"); //Stacks Stack <Customer> stackCustomers = new Stack <Customer>(); stackCustomers.Push(customer1); stackCustomers.Push(customer2); stackCustomers.Push(customer3); stackCustomers.Push(customer2); cntr = 0; foreach (var cust in stackCustomers) { cntr++; Console.WriteLine($"Listing of Customer no. {cntr} : {cust.Name}"); } Console.WriteLine("------------------------------"); Customer stackCqp = stackCustomers.Peek(); Console.WriteLine("Customer Name that was peeked: " + stackCqp.Name); Console.WriteLine("------------------------------"); //Removes from the bottom of the list Customer popCustomer = stackCustomers.Pop(); Console.WriteLine("Customer Name removed: " + popCustomer.Name); Console.WriteLine("-------------Stack Result after pop-----------------"); cntr = 0; foreach (var cust in stackCustomers) { cntr++; Console.WriteLine($"Listing of Customer no. {cntr} : {cust.Name}"); } }
// SORTING METHODS AND INTERFACE IMPLEMENTATIONS public void DefaultSort() { SortByName sbn = new SortByName(); pickedCities.Sort(sbn); }
public void orderList(object sender, EventArgs e) { ArrayList courselist = (ArrayList)Session["courselist"]; if (Session["selectedcourse"] != null && !Session["selectedcourse"].Equals("-1")) { Course course = (Course)courselist[int.Parse(Session["selectedcourse"].ToString())]; TableRow header = tblStudents.Rows[0]; tblStudents.Rows.Clear(); tblStudents.Rows.Add(header); ArrayList list = course.GetStudents(); if (Session["sort"] != null && (string)Session["sort"] == Request.Params["sort"]) { if (Session["order"] != null && Session["order"].ToString() == "ascending") { this.order = "descending"; Session["order"] = this.order; } else { this.order = "ascending"; Session["order"] = this.order; } } else { this.order = "ascending"; Session["order"] = this.order; } Session["sort"] = Request.Params["sort"]; IComparer comparer; switch (Request.Params["sort"]) { case "id": comparer = new SortById(this.order); list.Sort(comparer); break; case "name": comparer = new SortByName(this.order); list.Sort(comparer); break; case "grade": comparer = new SortByGrade(this.order); list.Sort(comparer); break; } foreach (Student student in list) { TableRow row = new TableRow(); TableCell cell = new TableCell(); cell.Text = student.ID; row.Cells.Add(cell); cell = new TableCell(); cell.Text = student.Name; row.Cells.Add(cell); cell = new TableCell(); cell.Text = student.Grade + ""; row.Cells.Add(cell); tblStudents.Rows.Add(row); } } }