- Home /
Problem with List Sorting (JavaScript)
Hello everyone well I've been messing with this for a while now and I can use this way to sort my items by thier name just fine but when it comes to sorting it by the Enum ItemTypes they just turn into the same item for some reason. Sorry i posted this before but it was in the wrong section :/
here is the code i'm using and where I'm trying to apply it.
//Main Inventory List
var mainInventoryList : List.<ItemHandler> = new List.<ItemHandler>();
function SortByName() {
mainInventoryList.Sort(function(a,b){
var nameA = a.itemType;
var nameB = b.itemType;
return nameA.CompareTo(nameB);
});
}
This is the for Loop I used which works perfectly for adding the items but the problem occurs when the sorting happens so would I need to move where I called the function somewhere? or how would I go about that.
function OnGUI() {
for(var x = 0; x < mainInventoryList.Count; x++) {
SortByName();
if(GUI.Button(Rect(0, 0 + (x * 38), 150, 32), GUIContent(" " +mainInventoryList[x].name ,mainInventoryList[x].icon))){
}
}
Item Handler Class
//This handles all of the properties of the items.
public class ItemHandler {
//The Id of the item.
var itemID : int;
//The icon for the item.
var icon : Texture2D;
//The name of the item.
var name : String;
//The Description of the item.
var description : String;
//This tells us if the item is equiped.
var equipped : boolean = false;
//This tells me if the item is selected.
var selected : boolean = false;
//The types of items.
var itemType : ItemType;
}
//The basic Constructor for our item ##WHEN MAKING AN ITEM ALL OF THESE PARAMETERS HAVE TO BE MET!!##
function ItemHandler(ID : int, ItemIcon : Texture2D, ItemName : String, ItemDescription : String, Equipped : boolean, Selected : boolean, Type : ItemType) {
this.itemID = ID;
this.icon = ItemIcon;
this.name = ItemName;
this.description = ItemDescription;
this.equipped = Equipped;
this.selected = Selected;
this.itemType = Type;
}
//The item types
//ItemType.Chest
enum ItemType {
Head,
Chest,
Gloves,
Legs,
Feet,
RightHand,
LeftHand,
Neckless,
HealingItem,
QuestItem,
MiscItem
}
For instance I want to group items with the item type Head together and so on.
Any help you could provide will help.
And please don't just send my a link to this if you do explain how I would go about finding out why I can sort by my items name and not by my items type. Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613141428im_/https://answers.unity.com/themes/thub/images/avi.jpg)