Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 bromley · Jun 01, 2016 at 05:32 PM · arraylistordersort

List.Sort with IComparer

i created a list but i want to sort it with a custom IComparer... the IComparer class is this

 public class Comp : IComparer<int>
 {
     public int Compare (int x, int y)
     {
         if (x == null) {
             if (y == null) {
                 return 0;
             } else {
                 return 1;
             }
 
         } else {
             if (y == null) {
                 return -1;
             } else {
                 int difference = x.CompareTo (y);
 
                 if (difference != 0) {
                     return difference;
                 } else {
                     return 0;
                 }
             }
         }
     }
 }

Now in the same script I call a function that Add objects in the list, but at the end I want sort the list

     Comp comparer = new Comp ();

 foreach (GameObject item in items) 
         {
             int i = Array.IndexOf (items, item);
             ItemData itemScript = item.GetComponent <ItemData> ();     
 
             yield return itemScript.itemState;
 
             itemSt.Add (new ItemStat (itemScript.ID, itemScript.itemState, item.name));
             if (items [i].name == itemSt [i].Name && itemSt [i].State == "Destroyed") 
             {
                 Destroy (item.gameObject, 0.0F);
             }
         }
 
         itemSt.Sort (comparer);

But the itemSt.Sort (comparer); line give two errors:

The best overloaded method match for System.Collections.Generic.List<ItemStat>.Sort(System.Collections.Generic.IComparer<ItemStat>) has some invalid arguments

Argument #1' cannot convert Comp' expression to type System.Collections.Generic.IComparer<ItemStat>'

I don't understand the problem... why i can't sort this list that contains variables from ItemStat class?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jun 01, 2016 at 05:39 PM

You create a comparer for the type "int" but you want to compare "ItemStat" objects. That doesn't make much sense.

Your comparer should look like this:

 public class Comp : IComparer<ItemStat>
 {
     public int Compare (ItemStat x, ItemStat y)
     {
         // your custom compare code here.
     }
 }

From your code it isn't clear what those ItemStat objects contain and based on what you want to sort them. So you have to implement the comparing code yourself or include how the ItemStat class looks like and how you want them to be sorted.

Comment
Add comment · Show 1 · Share
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
avatar image bromley · Jun 02, 2016 at 07:15 AM 0
Share

Yes, the problem was this... thanks

Now the script is:

 public class Comp : IComparer<ItemStat>
 {
     public int Compare (ItemStat x, ItemStat y)
     {
         if (x == null) {
             if (y == null) {
                 return 0;
             } else {
                 return 1;
             }
 
         } else {
             if (y == null) {
                 return -1;
             } else {
                 int difference = x.ID.CompareTo(y.ID);
 
                 if (difference != 0) {
                     return difference;
                 } else {
                     return 0;
                 }
             }
         }
     }
 }
avatar image
0

Answer by farhanblu · Jun 01, 2016 at 05:53 PM

If I were you, I would have a class (that I want sorted in list) implement IComparable interface. MSDN Reference

You could also consider using Linq if you want to avoid typing much code (i.e. implementing an interface for each comparable class).

MSDN LINQ : Order By

OrderBy Example

Comment
Add comment · Share
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

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

49 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to sort a list of gameobjects by their name? 4 Answers

What is the quickest way of getting the most common item in a list or array? 1 Answer

Show Top 10 Of 1 GameType 2 Answers

How to arrange list 3 Answers

How to sort a built-in array in the inspector? 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