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 martipello · Aug 30, 2017 at 01:51 PM · objectlistsorting

sort list of object and if items are the same use another vaue

hi im rather new to unity and c# im trying to sort a list of custom objects by a specific value, which works fine, but now i want to check if the value is the same and if it is sort the two objects by another value so for instance if all objects have int A and int B i want to sort the list by int A but if object 1 int A is the same as object 2 int A then sort these 2 objects by int B aswell, but only for these matching objects not the rest of the list (unless there is another tie) im sure there will be a built in function in list.sort to deal with this i just havent been able to find it, many 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

2 Replies

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

Answer by martipello · Aug 31, 2017 at 11:38 AM

i actually found a much more descriptive answer here https://www.dotnetperls.com/icomparable ive added an IComparable to my object class making it look like this

 using System.Collections;
 using System.Collections.Generic;
 using System;
 using UnityEngine;

 public class fighterObject : IComparable<fighterObject>
 {
 public int Universe { get; set; }
 public int KillCount { get; set; }
 public int JointKillCount { get; set; }
 public string Name { get; set; }
 public string Info { get; set; }
 public string JointKillInfo { get; set; }
 public Sprite Image { get; set; }

 public fighterObject(int universe, 
  int killcount, 
 int jointkillcount, 
 string name, 
 string info, 
 string  jointKillInfo, 
 Sprite image )
 {
     Universe = universe;
     KillCount = killcount;
     JointKillCount = jointkillcount;
     Name = name;
     Info = info;
     JointKillInfo = jointKillInfo;
     Image = image;
 }

 public int CompareTo(fighterObject other)
 {
     // Alphabetic sort if salary is equal. [A to Z]
     /*
      if (this.KillCount == other.KillCount && this.JointKillCount == other.JointKillCount)
     {
         return this.Name.CompareTo(other.Name);
     }
     */
     if (this.KillCount == other.KillCount)
     {
         return other.JointKillCount.CompareTo(this.JointKillCount);
     }
     // Default to salary sort. [High to low]
     return other.KillCount.CompareTo(this.KillCount);
 }
 public override string ToString()
 {
     // String representation.
     return this.KillCount.ToString() + " , " + this.JointKillCount.ToString() + " , " + this.Name;
 }

 }

which allows me to just call list.sort() on any list containing fighterObject

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

Answer by TheSOULDev · Aug 30, 2017 at 02:11 PM

You would need to use something like this:

 public class Vector : IComparer<Vector>
 {
     int A, B;
 
     public Vector (int a, int b)
     {
         A = a;
         B = b;
     }
 
     int IComparer<Vector>.Compare (Vector a, Vector b)
     {
         return (a.A == b.A) ? ((a.B == b.B) ? 0 : ((a.B > b.B) ? 1 : -1)) : ((a.A > b.A) ? 1 : -1);
     }
 }

I called this class Vector (because it is basically a Vector2). You implement an IComparer interface which tells the list how you sort this custom class - in the custom class, I used a huge ternary return value because, well, I think it's elegant, if you don't understand it just comment this and I'll give you the if-else variant. I also assume that you want the comparer to have an equal value if both the A and B are the same, I've included that.

Comment
Add comment · Show 3 · 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 martipello · Aug 31, 2017 at 06:48 AM 0
Share

Wow that is way over my head, does look elegant but id love an if else version if you wouldn't $$anonymous$$d, and can I swap out the vector for any dataset really as long as I'm comparing it properly?

avatar image martipello · Aug 31, 2017 at 06:50 AM 0
Share

And I hadn't even considered both values being equal lol many thanks

avatar image martipello · Aug 31, 2017 at 06:58 AM 0
Share

Ok just read up on ternarys and think I'll be ok but I'm not sure how I would call this right now I'm using list.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

72 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 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

A node in a childnode? 1 Answer

Sort list game objects based on name 3 Answers

Load Objects from a specific directory into a list? 0 Answers

Counting active objects in a list 3 Answers

How do I get List of all objects touching during a collision 3 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