## Insertion sort in one method using class object

insertion sort best case

insertion sort array of objects java

insertion sort arraylist strings java

insertion sort in java using for loop

insertion sort arraylist java

insertion sort using for loop

java collections insertion sort

I am building my lab for college and I am new to .net framwork. I wanna to sort the data but I need to follow certain rule for this lab one of them is I can not use list it has to be array.

Here is my menu

1 - Sort by Employee Name (ascending) 2 - Sort by Employee Number (ascending) 3 - Sort by Employee Pay Rate (descending) 4 - Sort by Employee Hours (descending) 5 - Sort by Employee Gross Pay (descending) 6 - Exit

I have a data in model class and I store data in my employees array. I shorted my menu 2 but I need global function for all menu.

**Sorting code**

for (int i = 1; i < employees.Length; i++) { Employee current = employees[i]; int j = i - 1; for (; j >= 0 && current.GetNumber() > employees[j].GetNumber(); j--) { employees[j + 1] = employees[j]; } employees[j + 1] = current; } foreach (Employee employee in employees) { Console.WriteLine(employee); }

Question : Is there any way that I can make one function that gives me different output for each menu because It is criteria that i can use one function to complete the all menu.

If need anything from my side please comment.

You can use delegates to solve your problem. So you will have a common method to sort your array that will also accept the delegate on how to compare 2 Employees:

public Employee[] Sort(Employee[] employees, Func<Employee, Employee, bool> comparer) { for (int i = 1; i < employees.Length; i++) { Employee current = employees[i]; int j = i - 1; for (; j >= 0 && comparer(current,employees[j]); j--) { employees[j + 1] = employees[j]; } employees[j + 1] = current; } return employees; }

And an example of usage will be

Sort(employees, (e1, e2) => e1.GetNumber() > e2.GetNumber())

or

Sort(employees, (e1, e2) => string.Compare(e1.Name, e2.Name) < 0)

**Insertion sort with an array of objects?,** You can call the function with an Integer[] , because Integer is comparable--it does create separate methods for each sort attribute in the InsertionSort class. One is to pass to the sort method a Comparator that provides methods for comparing the Objects in the a[] array. A second method is to require that each object in the a[] array be responsible for being able to determine its precedence with respect to any other object in the a[] array.

I changed your code. One can retrieve property and then use it:

public void Sort(Employee[] employees, string propertyName) { var desiredProperty = typeof(Employee).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); PropertyInfo info = typeof(Employee).GetProperty(propertyName); if (info == null) { return; } for (int i = 1; i < employees.Length; i++) { Employee current = employees[i]; int j = i - 1; int curValue = Convert.ToInt32(info.GetValue(current)); int prevValue = Convert.ToInt32(info.GetValue(employees[j])); for (; j >= 0 && curValue > prevValue; j--) { employees[j + 1] = employees[j]; } employees[j + 1] = current; } foreach (Employee employee in employees) { Console.WriteLine(info.GetValue(employee)); } }

**Insertion sort in one method using class object,** You can use delegates to solve your problem. So you will have a common method to sort your array that will also accept the delegate on how to compare 2 Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. Algorithm. // Sort an arr[] of size n. insertionSort(arr, n) Loop from i = 1 to n-1.

create extention class

public static class OrderedEnumerable { public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer = null) { if (source == null) { throw new ArgumentNullException(); } if (keySelector == null) { throw new ArgumentNullException(); } if (comparer == null) { comparer = Comparer<TKey>.Default; } Comparison<TSource> comparer2 = (x, y) => comparer.Compare(keySelector(x), keySelector(y)); return new OrderedEnumerableImpl<TSource>(source, comparer2); } public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer = null) { if (source == null) { throw new ArgumentNullException(); } if (keySelector == null) { throw new ArgumentNullException(); } if (comparer == null) { comparer = Comparer<TKey>.Default; } Comparison<TSource> comparer2 = (x, y) => comparer.Compare(keySelector(y), keySelector(x)); return new OrderedEnumerableImpl<TSource>(source, comparer2); } private class OrderedEnumerableImpl<TSource> : IOrderedEnumerable<TSource> { private readonly IEnumerable<TSource> Source; private readonly Comparison<TSource> Comparer; public OrderedEnumerableImpl(IEnumerable<TSource> source, Comparison<TSource> comparer) { Source = source; Comparer = comparer; } public IOrderedEnumerable<TSource> CreateOrderedEnumerable<TKey>(Func<TSource, TKey> keySelector, IComparer<TKey> comparer, bool descending) { if (comparer == null) { comparer = Comparer<TKey>.Default; } Comparison<TSource> comparer2; if (descending) { comparer2 = (x, y) => { int result = Comparer(x, y); if (result == 0) { result = comparer.Compare(keySelector(y), keySelector(x)); } return result; }; } else { comparer2 = (x, y) => { int result = Comparer(x, y); if (result == 0) { result = comparer.Compare(keySelector(x), keySelector(y)); } return result; }; } return new OrderedEnumerableImpl<TSource>(Source, comparer2); } public IEnumerator<TSource> GetEnumerator() { var source = Source.ToArray(); // ** Here you do the sorting! ** Array.Sort(source, Comparer); for (int i = 0; i < source.Length; i++) { yield return source[i]; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }

and Use like as follow

public class Employee { public int Number { get; set; } public int GrossPay { get; set; } public int Hours { get; set; } public int PayRate { get; set; } public string Name { get; set; } } void Main() { var Employee = new Employee[] { new Employee { Name = "acb", Number = 123 }, new Employee { Name = "nmo", Number = 456 }, new Employee { Name = "xyz", Number = 789 } }; var sortedEmpByNum = Employee.OrderBy(x => x.Number);// by number asc var sortedEmpByNubDesc = Employee.OrderByDescending(x => x.Number); //by number desc var sortedEmpByName = Employee.OrderBy(x => x.Name); //by name asc //you shoud use this one var finalResult = Employee.OrderBy(x => x.Name) .ThenBy(x => x.Number) .ThenByDescending(x => x.PayRate) .ThenByDescending(x => x.Hours) .ThenByDescending(x => x.GrossPay); }

credit=> custom-lambda-sort-extension

**Data Structure and Algorithms Insertion Sort,** Insertion Sort is a simple sorting algorithm that works wonders on small arrays. Insertion Sort is one of the simpler sorting algorithms, which works considerably faster the algorithm on primitive integer arrays and a collection of objects with a custom public class Element { private int id; public Element(int id) { this.id = id; } 1. Sort objects using insertion sort. In our previous article we had learnt about insertion sort. At each iteration, insertion sort removes one element from the input array and insert that element in a location it belongs within the sorted list. And this is repeated until there are no elements remaining in the input array.

**Insertion Sort,** Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. Algorithm // Sort an arr[] of size n insertionSort(arr, n) Loop from i = 1 to n-1. ……a) Pick element class InsertionSort {. /*Function to sort array Insertion sort is a simple and efficient comparison sort. In this algorithm, each iteration removes an element from the input data and inserts it into the correct position in the list is sorted. The choice of the element is removed from the input is random and this process is repeated until all input elements have gone through.

**Insertion Sort in Java,** Regular Expressions in Java · JDBC Using Model Object and Singleton Class Insertion sort is a simple sorting algorithm that works the way we sort playing class InsertionSort. {. /*Function to sort array using insertion sort*/ int j = i- 1 ;. /* Move elements of arr[0..i-1], that are. greater than key, to one position ahead. Arguably the best way to use Insertion Sort for custom classes is to pass another argument to the insertion_sort method - specifically a comparison method. The most convenient way to do this is by using a custom lambda function when calling the sorting method.

**Java Program for Insertion Sort,** Insertion Sort is a simple and slow sorting algorithm that repeatedly takes the next Take an element from the un-sorted section. Insert public class InsertionSortExample public static void insertionSort(Object[] a, int fromIndex, int toIndex). Insertion sort is a simple sorting algorithm suited for small data sets. During each iteration, the algorithm. Removes an element from an array. Compares it against the largest value in the array. Moves the element to its correct location. Here is how the process works graphically. JAVA program to sort an array using Insertion sort algorithm.

##### Comments

- You should have to use LINQ.
- Thank you for quick reply. I found solution on internet but it's in list or LINQ. But i am looking for array.
- I would suggest that the best solution would be to define an
`EmployeeComparer`

class that implements`IComparer(Of Employee)`

and allows you to specify which property to sort by. You can then simply create an instance of that class and specify the desired property, then pass it to`Array.Sort`

.