Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mrslayer01 · Mar 28, 2014 at 09:03 PM · javascriptlist

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

A node in a childnode? 1 Answer

list of pre defined functions like Update and OnTriggerEnter to use 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges