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
3
Question by BilboStabbins · Jul 01, 2013 at 07:37 PM · sortinglinqtoarray

Sorting an array using Linq and OrderBy

I'm trying to sort an array of waypoint object each starting with 'WP' followed by a number from 1 - 9. The waypoints have a parent object which holds the script and each contain a spawnpoint object as a child. Each of the waypoints also have a 'Waypoint' script attached which is how I'm adding them to an array to begin with.

The code also gives the following error:

 ArgumentException: does not implement right interface
 System.Collections.Generic.Comparer`1+DefaultComparer[UnityEngine.GameObject].Compare (UnityEngine.GameObject x, UnityEngine.GameObject y)
 System.Linq.SortSequenceContext`2[Waypoint,UnityEngine.GameObject].Compare (Int32 first_index, Int32 second_index)
 System.Linq.QuickSort`1[Waypoint].CompareItems (Int32 first_index, Int32 second_index)
 System.Linq.QuickSort`1[Waypoint].MedianOfThree (Int32 left, Int32 right)
 System.Linq.QuickSort`1[Waypoint].Sort (Int32 left, Int32 right)
 System.Linq.QuickSort`1[Waypoint].PerformSort ()
 System.Linq.QuickSort`1+<Sort>c__Iterator21[Waypoint].MoveNext ()
 System.Collections.Generic.List`1[Waypoint].AddEnumerable (IEnumerable`1 enumerable)
 System.Collections.Generic.List`1[Waypoint]..ctor (IEnumerable`1 collection)
 System.Linq.Enumerable.ToArray[Waypoint] (IEnumerable`1 source)
 WPGroup.Start () (at Assets/016 - Move Object Randomly with Radius/WPGroup.cs:17)
 

The code I am working with is as follows;

 using System.Linq;
 
 public Waypoint[] WPGroupList = new Waypoint[9];
     
 public GameObject[] WPgoGroupList = new GameObject[9];
     
 // Use this for initialization
 void Start () 
 {
     // Searches entire scene - just want to search this group
     //WPGroupList = GameObject.FindGameObjectsWithTag("Waypoint");
 
     // Pass all waypoints inside object into array
     WPGroupList = gameObject.GetComponentsInChildren<Waypoint>(); // Select(go => go.transform).ToArray();
     
     // Order array in ascending order - not working
     WPGroupList.OrderBy(go => go.name).ToArray();       
 
     // Created array of GameObjects afterwards to test if problem was that it was 
     // having trouble because they were waypoints
     for (int i = 0; i < WPGroupList.Length; i++)
     {
          // Copy complete gameObjects from WP list into this GameObject array
          WPgoGroupList[i] = WPGroupList[i].gameObject;
     }
 
       // Try to sort that - not working
     //WPgoGroupList.OrderBy(go=>go.name).ToArray();
 }
Comment
Add comment · Show 1
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 BlueRaja_2014 · Jul 01, 2013 at 08:15 PM 0
Share

According to that error, you are trying to sort over the GameObjects themselves, which won't work since they don't implement IComparable. The code you have pasted is not where the error is occurring...

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Jessy · Jul 01, 2013 at 08:03 PM

OrderBy is not a function that you call on a collection in place. I don't know why it compiles for me; it doesn't do anything. This is how LINQ works:

 This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

You have to assign back to the array, if you even indeed need an array:

 WPGroupList = WPGroupList.OrderBy(go => go.name).ToArray();   

Rename your variables, though. PascalCase is not for fields. Also, I can't get your particular error to show up.

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 turbanov · Jan 12, 2016 at 06:28 PM 1
Share

PascalCase is ok for public fields/properties. It's actually a $$anonymous$$icrosoft's way of doing things according to this: https://msdn.microsoft.com/en-us/library/ms229043(v=vs.110).aspx

avatar image
2

Answer by raiden · May 12, 2014 at 06:39 AM

One issue with using Linq to sort with OrderBy, if you have more than 9 objects, say 1,2,3,4,5,6,7,8,9,10, Linq will sort them as 1,10,2,3,4,5,6,7,8,9 which is most likely not what you want.

The fix for this, is simply:

 WPGroupList = WGroupList(go=>int.Parse(go.name.Substring(9))).ToArray();

Where 9 represents the substring section of where you named your index number on your waypoint game object name (exe. Waypoint-9).

Hope that helps you further.

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 bellwether · Sep 25, 2017 at 05:19 PM 0
Share

This is very late but I tried to implement your code as I have more than 9 objects but the numbers are still jumbled as you've posted. Could you look at my post and help me. http://answers.unity3d.com/questions/1411493/linq-orderby-help.html

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

Sorting a List by Variable? 2 Answers

sorting list by name 2 Answers

JIT exception with ToArray() Linq 2 Answers

Confusion over LINQ syntax 2 Answers

Sort a generic list by member 1 Answer


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