• 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 Ace71425 · Apr 15, 2018 at 06:11 AM · arraysclasssortingordering

Sort arrays using OrderBy and ThenBy Linq

Im at about my wits end here...Here's the scoop.

I have 4 int arrays we'll call them a, b, c, d for ease sake. Here's what they look like...

 a   b   c    d
 3   2  20  25
 5   3  30  20
 5   3  32  21
 6   4  40  15

What I need to do is sort them based on the methodology of OrderBy and ThenBy in Linq....So for instance I need to sort the A array $$anonymous$$ghest to lowest (A takes precedence), but, then if values in the A array are the same I need to then sort those same values by the B array to find w$$anonymous$$ch A value is really $$anonymous$$ghest and then if the B values are the same move on to C so on and so forth.

So in t$$anonymous$$s scenario the correct sorting should be....

 a   b   c    d
 6   4  40  15
 5   3  32  21
 5   3  30  20
 3   2  20  25

Here's some sample code because I have to make a class, then set the class values because I can only make a list from a class and then make a OrderBy from a list (so I t$$anonymous$$nk)

 public class daClass
     {
         public int[] wfilter, dwfilter, pffilter, pafilter;
     }
 
 daClass daValues = new daClass();
 
         daValues.wfilter = wstrip;
         daValues.dwfilter = dwstrip;
         daValues.pffilter = pfstrip;
         daValues.pafilter = pastrip;
 
         var arrays = new List<daClass>();
 
         var orderedList = arrays.OrderBy(p => p.wfilter).ThenBy(p => p.dwfilter).ThenBy(p => p.pffilter)
             .ThenBy(p => p.pafilter).ToArray();

As much as I feel I've made progress I kind of feel like I'm all over the place here. T$$anonymous$$s seems easy but I'm just brain freezing on it. I don't know if I should be doing t$$anonymous$$s by class or if my instance of the class is even filling in values to the class or if the list is even grabbing said values or the sorting is even sorting it correctly...I can't find any way to debug.log the class or the list or the orderedlist. Some suggestions on how to do t$$anonymous$$s would be MUCH appreciated....Thanks guys!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Summit_Peak · Apr 15, 2018 at 06:03 PM

Try ordering by items in the array rather than ordering the arrays:

 var orderedList = daValues.OrderBy(x => x[0]).ThenBy(y => y[1]).ThenBy(z => z[2]).ThenBy(w => w[3]).ToList();
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Prioritizing objects in an array based on a variable value? 0 Answers

C# Class and Array 1 Answer

Vector3 resultant array sorting 2 Answers

Need recommendation for a sorting method for Enemy distances from Player (Ascending) - C# 1 Answer

Problem with arrays in a list 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