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

People who like this

0 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
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
NatCou
AlejandroBoss10

People who like this

2 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

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

Use the List.Sort() method

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Trouble Getting Array List To Display Current Song In Game 0 Answers

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers

Add value to List without replacing previous 0 Answers

A node in a childnode? 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