public void SetSoringMode(string sortingMode) { switch (sortingMode) { case "Num. Ascending": compareItems = AscendingAmount; break; case "Num. Descending": compareItems = DescendingAmount; break; case "Alph. Ascending": compareItems = AscendingName; break; case "Alph. Descending": compareItems = DescendingName; break; default: Debug.Log("Sorting mode in drop down is not recognised, check drop down option names"); break; } }
void Start() { inventoryList = new List <InventoryItemScript>(); for (int i = 0; i < itemNames.Count; i++) { //Create a duplicate of the starter item GameObject inventoryItem = (GameObject)Instantiate(itemPrefub); // UI items need to parented by the canvas or an object within the canvas inventoryItem.transform.SetParent(parentPanel); // Original start item is disabled – so the duplicate must be enabled inventoryItem.SetActive(true); // Get InventoryItemScript component so we can set the data InventoryItemScript iis = inventoryItem.GetComponent <InventoryItemScript>(); iis.itemNameText.text = itemNames[i]; iis.itemName = itemNames[i]; iis.itemAmountText.text = itemAmounts[i].ToString(); iis.itemAmount = itemAmounts[i]; //Keep a list of inventory items inventoryList.Add(iis); } DisplayListInOrder(); compareItems = AscendingName; }
public void CompareItems_SortValueIsLessThan_ReturnsZeroIfValuesAreEqual() { Date date1 = new Date(2010, 10, 10); Date date2 = new Date(2010, 10, 10); IList <Tuple <Value, bool> > list1 = new List <Tuple <Value, bool> >() { new Tuple <Value, bool>(Value.Get(date1), false) }; IList <Tuple <Value, bool> > list2 = new List <Tuple <Value, bool> >() { new Tuple <Value, bool>(Value.Get(date2), false) }; Assert.Equal(0, CompareItems <Post> .SortValueIsLessThan(list1, list2)); Assert.Equal(0, CompareItems <Post> .SortValueIsLessThan(list2, list1)); }
public void CompareItems_SortValueIsLessThan_ReturnsMinusOneIfFirstLessThanSecondOrOneOtherwiseWhenInverted() { Date date1 = new Date(2010, 10, 10); Date date2 = new Date(2010, 10, 15); IList <Tuple <Value, bool> > list1 = new List <Tuple <Value, bool> >() { new Tuple <Value, bool>(Value.Get(date1), true) }; IList <Tuple <Value, bool> > list2 = new List <Tuple <Value, bool> >() { new Tuple <Value, bool>(Value.Get(date2), true) }; Assert.Equal(-1, CompareItems <Post> .SortValueIsLessThan(list1, list2)); Assert.Equal(1, CompareItems <Post> .SortValueIsLessThan(list2, list1)); }
public void CompareItems_SortValueIsLessThan_IgnoresBalancesAndReturnsZero() { Balance bal1 = new Balance(new Amount(10)); Balance bal2 = new Balance(new Amount(10)); IList <Tuple <Value, bool> > list1 = new List <Tuple <Value, bool> >() { new Tuple <Value, bool>(Value.Get(bal1), false) }; IList <Tuple <Value, bool> > list2 = new List <Tuple <Value, bool> >() { new Tuple <Value, bool>(Value.Get(bal2), false) }; Assert.Equal(0, CompareItems <Post> .SortValueIsLessThan(list1, list2)); Assert.Equal(0, CompareItems <Post> .SortValueIsLessThan(list2, list1)); }
/// <summary> /// Waits for arrival of an item for <param name="timeout"> milliseconds. </param> /// </summary> /// <param name="comparer">The comparator for indentifying the wanted item</param> /// <param name="timeout">The time in milliseconds to wait. set it to -1 for infinite</param> /// <returns></returns> public T WaitFor(CompareItems comparer, int timeout = -1) //todo test it { T res = default(T); //ignore default(T), it doesnt matter anyway var firstOrDefault = this.FirstOrDefault(t => comparer(t)); if (firstOrDefault != null && firstOrDefault.Equals(default(T)) == false) { return(firstOrDefault); } //not found.. wait for it. var waiter = new ManualResetEventSlim(false); var itemAddedHandler = new ItemAddedHandler(item => { if (comparer(item)) { res = item; waiter.Set(); } }); ItemAdded += itemAddedHandler; var fod = this.FirstOrDefault(t => comparer(t)); if (fod != null && fod.Equals(default(T)) == false) { ItemAdded -= itemAddedHandler; return(fod); } if (timeout > -1) { waiter.Wait(timeout); } else { waiter.Wait(); } return(res); }
List <InventoryItemScript> MergeSort(List <InventoryItemScript> list, CompareItems method) { //recursion exit point if (list.Count <= 1) { return(list); } //recursive step List <InventoryItemScript> leftList = new List <InventoryItemScript>(); List <InventoryItemScript> rightList = new List <InventoryItemScript>(); for (int i = 0; i < list.Count; i++) { if (i < list.Count / 2) { leftList.Add(list[i]); } else { rightList.Add(list[i]); } } //print(leftList.Count); //print("------"); //print(rightList.Count); //print("######"); leftList = MergeSort(leftList, method); rightList = MergeSort(rightList, method); //post recursion step list = Merge(leftList, rightList, method); //print("Merged list"); return(list); }
List <InventoryItemScript> Merge(List <InventoryItemScript> leftList, List <InventoryItemScript> rightList, CompareItems method)// ,DelegatePointer) { List <InventoryItemScript> merged = new List <InventoryItemScript>(); while (leftList.Count > 0 && rightList.Count > 0) { if (method(leftList[0], rightList[0])) //if (Order) { merged.Add(leftList[0]); leftList.RemoveAt(0); } else { merged.Add(rightList[0]); rightList.RemoveAt(0); } } while (leftList.Count > 0) { merged.Add(leftList[0]); leftList.RemoveAt(0); } while (rightList.Count > 0) { merged.Add(rightList[0]); rightList.RemoveAt(0); } return(merged); }
List<InventoryItemScript> MergeSort(List<InventoryItemScript> list, CompareItems method) { //recursion exit point if (list.Count <= 1) return list; //recursive step List<InventoryItemScript> leftList = new List<InventoryItemScript>(); List<InventoryItemScript> rightList = new List<InventoryItemScript>(); for (int i = 0; i < list.Count; i++) { if (i < list.Count / 2) { leftList.Add(list[i]); } else { rightList.Add(list[i]); } } //print(leftList.Count); //print("------"); //print(rightList.Count); //print("######"); leftList = MergeSort(leftList, method); rightList = MergeSort(rightList, method); //post recursion step list = Merge(leftList, rightList, method); //print("Merged list"); return list;
// ,DelegatePointer) List<InventoryItemScript> Merge(List<InventoryItemScript> leftList, List<InventoryItemScript> rightList, CompareItems method) { List<InventoryItemScript> merged = new List<InventoryItemScript>(); while (leftList.Count > 0 && rightList.Count > 0) { if (method(leftList[0], rightList[0])) //if (Order) { merged.Add(leftList[0]); leftList.RemoveAt(0); } else { merged.Add(rightList[0]); rightList.RemoveAt(0); } } while (leftList.Count > 0) { merged.Add(leftList[0]); leftList.RemoveAt(0); } while (rightList.Count > 0) { merged.Add(rightList[0]); rightList.RemoveAt(0); } return merged;
public List <InventoryItemScript> MergeLists(List <InventoryItemScript> list1, List <InventoryItemScript> list2, CompareItems ci) { List <InventoryItemScript> list = new List <InventoryItemScript>(); int list1Index = 0, list2Index = 0; while (list1Index < list1.Count && list2Index < list2.Count) { if (ci(list1[list1Index], list2[list2Index])) { list.Add(list1[list1Index]); list1Index++; } else { list.Add(list2[list2Index]); list2Index++; } } if (list1Index < list1.Count) { list1.RemoveRange(0, list1Index); list.AddRange(list1); } else { list2.RemoveRange(0, list2Index); list.AddRange(list2); } return(list); }