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 hijinxbassist · Jun 14, 2012 at 06:33 PM · listsortingsort

Sorting a jagged list based on 2nd entry

Hi, having trouble figuring out a good way to sort my jagged list, each base list contains 6 values. I need to sort the list based on the 2nd value.

Heres an example of what might be in the list

 john 2 4 150 ...
 sam  4 2 402 ...
 kel  1 4 233 ...
 ida  3 1 523 ...

I want to sort it so it looks like, the 2nd value being compared.

 kel  1 4 233 ...
 john 2 4 150 ...
 ida  3 1 523 ...
 sam  4 2 402 ...

Im kinda lost trying to figure out how to sort a jagged list, if someone can help id be more than greatful. Thanks!

Comment
Add comment · Show 3
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 whydoidoit · Jun 14, 2012 at 06:34 PM 0
Share

What language?

avatar image hijinxbassist · Jun 14, 2012 at 06:37 PM 0
Share

Unity script, but i dont $$anonymous$$d C# or boo as i can read both, ill just rewrite the whole script to whatever language since its only ~60 lines so far

avatar image whydoidoit · Jun 14, 2012 at 06:46 PM 0
Share

Sorry, posted a second, much better answer :)

1 Reply

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

Answer by whydoidoit · Jun 14, 2012 at 06:45 PM

Sorry first answer was a bit of madness!!!

Right well if it was a List of Lists of Object in c#:

 using System.Collections.Generic;
 using System.Linq;

   ...
   var myBaseList = new List<List<object>>();
   var sorted = myBaseList.OrderBy(c=>(int)c[1]).ToList();


Or an array list of array lists:

  using System.Linq;

   var sorted = myBaseList.OrderBy(c=>(int)((ArrayList)c)[1]).ToArray();

Which gives you an array of array lists.

Comment
Add comment · Show 12 · 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 whydoidoit · Jun 14, 2012 at 06:48 PM 0
Share

You can drop the ToArray and ToList if you are just going to do a foreach....

avatar image hijinxbassist · Jun 14, 2012 at 06:48 PM 0
Share

That was fast, Thanks! Looks like a mix of C# and Unity script to me(mainly the var declaration)

avatar image whydoidoit · Jun 14, 2012 at 06:49 PM 0
Share

Hey ask me about 3D math or shaders and it will take a LOT longer :)

I'm a Linq junkie...

avatar image whydoidoit · Jun 14, 2012 at 06:53 PM 0
Share

Well those were local variables rather than class level ones... I hate typing syntax twice.

avatar image whydoidoit · Jun 14, 2012 at 06:54 PM 0
Share

But you are right, if myBaseList was a class level variable - which I bet it will be then yep:

   List<List<object>> myBaseList = new List<List<object>>();

The sorted one can stay as var of course.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

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

Sorting a list of GameObjects by accessing their int values 2 Answers

Sorting Game Object Name In Numerical and Alphabetical Order via List 2 Answers

How to alphabetically sort your List? 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