public void SortByPrice() { MergeSort mergeSorter = new MergeSort(); IDroid[] droidCollectionTrimmed = new IDroid[lengthOfCollection]; for (int a = 0; a < lengthOfCollection; a++) { //This weeds out all of the null values, that mess up the merge sort if (droidCollection[a] != null) { droidCollectionTrimmed[a] = droidCollection[a]; } } droidCollection = (IDroid[])mergeSorter.sortArray(droidCollectionTrimmed); }
//*******************END METHOD TO ADD NEW DROID TO COLLECTION**************// //******************METHOD WHICH WILL MERGE SORT DROID COLLECTION BY TOTAL COST******************// public void MergeSort() { MergeSort merge = new MergeSort(); //create new instance of MergeSort class merge.sort(droidCollection, highestIndex); //pass in the droid collection, along with the highest index of the array. } //without the highestIndex, the sort could break due to possible null values in the array.
//Method to sort by total cost public bool Sort() { //Call MergeSort's constructor which automatically sorts it. MergeSort mergeSort = new MergeSort(this.droidCollection, this.lengthOfCollection); return true; }
static void Main(string[] args) { //Create a new droid collection and set the size of it to 100. IDroidCollection droidCollection = new DroidCollection(100); //Create a user interface and pass the droidCollection into it as a dependency UserInterface userInterface = new UserInterface(droidCollection); //Creates a merge sort class. MergeSort mergeSort = new MergeSort(); //Example droids for use in testing the program. //9 droids to make sure it works even with odd numbered arrays/lists. droidCollection.Add("Carbonite", "Utility", "Bronze", true, true, true); droidCollection.Add("Carbonite", "Protocol", "Bronze", 3); droidCollection.Add("Carbonite", "Astromech", "Bronze", true, true, true, true, 5); droidCollection.Add("Carbonite", "Janitor", "Bronze", true, true, true, true, true); droidCollection.Add("Carbonite", "Utility", "Silver", true, true, true); droidCollection.Add("Carbonite", "Protocol", "Silver", 3); droidCollection.Add("Carbonite", "Astromech", "Silver", true, true, true, true, 5); droidCollection.Add("Carbonite", "Janitor", "Silver", true, true, true, true, true); droidCollection.Add("Vanadium", "Protocol", "Gold", 6); //Display the main greeting for the program userInterface.DisplayGreeting(); //Display the main menu for the program userInterface.DisplayMainMenu(); //Get the choice that the user makes int choice = userInterface.GetMenuChoice(); //While the choice is not equal to 5, continue to do work with the program while (choice != 5) { //Test which choice was made switch (choice) { //Choose to create a droid case 1: userInterface.CreateDroid(); break; //Choose to Print the droid case 2: userInterface.PrintDroidList(); break; //Choose to sort the list by droid type case 3: droidCollection.SortByDroid(); break; //Chose to sort the list by droid total price case 4: droidCollection.SortByPrice(); break; } //Re-display the menu, and re-prompt for the choice userInterface.DisplayMainMenu(); choice = userInterface.GetMenuChoice(); } }
//*******************END METHOD TO ADD NEW DROID TO COLLECTION**************// //******************METHOD WHICH WILL MERGE SORT DROID COLLECTION BY TOTAL COST******************// public void MergeSort() { MergeSort merge = new MergeSort(); //create new instance of MergeSort class merge.sort(droidCollection, highestIndex); //pass in the droid collection, along with the highest index of the array. }
public void SortByCost() { MergeSort.Sort(droidCollection, lengthOfCollection); }
//This method utilizes the MergeSort class to perform a merge sort on the DroidCollection //and return the collection sorted by total cost public void CostSort() { RemoveNull(); MergeSort.SortAndMerge(droidCollection); }
//This method calls the merge sort on the droid collection. public void DroidMergeSort() { MergeSort.Sort(droidCollection, SizeCheck()); }
//********************** //MergeSort //********************** public void MergeSort() { MergeSort merge = new MergeSort(); merge.sort(droidCollection, lengthOfCollection); }
public void SortbyTotalCost() { MergeSort mergeSort = new MergeSort(); Droid[] droids = new Droid[LengthofCollection]; mergeSort.sort(droids); }