Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by 4illeen · Aug 12, 2011 at 11:29 AM · listsort

sort List alphabetically

a) I've got a List<Item>. Class Item has a string _name variable. I need to sort that list alphabetically according to _name.

b) I'd love to know how to do the same thing with int variables of Item class - sort the list according to them in increasing order.

I'm using C#. If anyone could write a little example for me to better understand this, I'd be very grateful, english is not my first language and reading documentation doesn't always work for me.

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

2 Replies

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

Answer by Blankzz · Aug 12, 2011 at 12:03 PM

No point me rewriting examples that you can find elsewhere on the internet. Try this link for example.

http://www.dotnetperls.com/sort-string-array

Edit: To get to the comparison on a class you need to pass in a comparison method like so;

Option1

 people.Sort(
     delegate(Item i1, Item i2) 
     { 
         return i1.name.CompareTo(i2.name); 
     }
 );

Option2

 list.Sort(CompareListByName);
 
 private static int CompareListByName(Item i1, Item i2)
 {
     return i1.name.CompareTo(i2.name); 
 }
Comment
Add comment · Show 4 · 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 4illeen · Aug 12, 2011 at 12:51 PM 0
Share

that works only for string arrays and lists - I've got List where Class holds the string.

avatar image Blankzz · Aug 12, 2011 at 01:23 PM 0
Share

O sorry missed that part. You need to sort the method using a comparison. http://msdn.microsoft.com/en-us/library/3da4abas.aspx http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx

The following should work for you

list.Sort(delegate(Item i1, Item i2) { return i1.name.CompareTo(i2.name); });

avatar image castor · Jul 23, 2013 at 04:47 AM 0
Share

how would I write this in javascript? I can't seem to find the right sintax

avatar image Fattie · Mar 05, 2014 at 07:14 AM 0
Share

http://answers.unity3d.com/questions/426142/sorting-gameobjects-i-an-array-alphabetically.html

avatar image
0

Answer by smacbride · Apr 20, 2016 at 07:51 PM

Can't you use a SortedList<> instead of List<> to hold your objects?

By default when you iterate the list it is sorted.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Sorting a list by distance to an object? 1 Answer

How to sort a list of gameobjects by their name? 4 Answers

sort a List of GameObjects by distance? 1 Answer

How to add and sort a list of gameobjects by tag? 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