Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Apr 12, 2019 at 04:52 AM by jopzsanji for the following reason:

Duplicate Question

avatar image
0
Question by jopzsanji · Feb 22, 2013 at 12:55 PM · c#listarraylistsort

How to alphabetically sort your List?

Here's my code


using System.Collections.Generic;

public class DotHolder : MonoBehaviour {

 public List myList = new List();
 
 void Start () {
     foreach (Transform child in gameObject.transform)
     {
       myList.Add(child.gameObject);
     }
 }

}


now that myList contains a gameobjects of it's children, my question is,

how can i sort all the children alphabetically?

example current output:

myList[0] = "dog4" myList[1] = "dog5" myList[2] = "dog3" myList[3] = "dog1" myList[4] = "dog5"

example target output:

myList[0] = "dog1" myList[1] = "dog2" myList[2] = "dog3" myList[3] = "dog4" myList[4] = "dog5"

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 Graham-Dunnett ♦♦ · Feb 22, 2013 at 01:49 PM 0
Share

http://video.unity3d.com/video/7720450/tutorials-using-unity-answers tells you how to format code.

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by Graham-Dunnett · Feb 22, 2013 at 02:01 PM

 // c# example
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class sortlist : MonoBehaviour {
 
     public List<string> myList = new List<string>();
  
     void Start () {
         myList.Add("Tyrannosaurus");
         myList.Add("Amargasaurus");
         myList.Add("Mamenchisaurus");
         myList.Add("Deinonychus");
         myList.Add("Compsognathus");
 
         foreach(string dinosaur in myList)
         {
             Debug.Log(dinosaur);
         }
 
         myList.Sort();
 
         foreach(string dinosaur in myList)
         {
             Debug.Log(dinosaur);
         }
 
 
     }
 }

MSDN is your friend.

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 ina · Mar 05, 2014 at 12:40 AM 0
Share

what about for non-string lists?

avatar image AlejandroBoss10 · Mar 08, 2021 at 04:59 AM 0
Share

Hey there, thanks for this, it's actually very helpful. I really appreciate it!

avatar image AbandonedCrypt AlejandroBoss10 · Mar 08, 2021 at 12:38 PM 1
Share

Super outdated, use Linq instead.

with using System.Linq; you get a bunch of useful extension methods for collections, such as the OrderBy and OrderByDescending methods.
Use them like this:

 List<GameObject> objSortedList = objListObject.OrderBy(o=>o.name).ToList();






avatar image
0

Answer by nullstar · Feb 22, 2013 at 01:56 PM

Use the List.Sort() method

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

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Is it possible to sort an array determined by an external comparison? 1 Answer

Combine Children Dictionary in place of Hashtable? 2 Answers

Sort an ArrayList of game objects by another script variable 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 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