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
0
Question by 12boulla · Dec 17, 2015 at 08:18 PM · liststringorder

Ordering a string list in size order

Hello,

I have a List<string> and it contains a lot of strings of different lengths. I need to re order the list in size order so the smallest strings are first, and then the bigger ones last. Does anyone know how i can do this?

Any help is greatly appreciated, thanks.

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
1

Answer by phil_me_up · Dec 17, 2015 at 08:48 PM

You probably want to use the IComparable interface and use the length of the string as your comparison (in your CompareTo function)

https://msdn.microsoft.com/en-us/library/system.icomparable(v=vs.110).aspx

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

Answer by Jessespike · Dec 17, 2015 at 09:14 PM

There are several ways. Implementing IComparable would work. Another way is to use Linq to sort:

 using System.Linq;

 List<string> SortByLength(List<string> stringList)
 {
     var sortedStringList = stringList
         .OrderBy(n => n.Length)
         .ToList();
 
     return sortedStringList;
 }

You'd just need to do this:

  myStringList = SortByLength(myStringList);

Another way, which isn't very pretty, but would work without Linq is this:

 List<string> SortStrings(List<string> stringList)
 {
     for(int i = 0; i < stringList.Count; i++)
     {
         for (int j = 0; j < stringList.Count; j++)
         {
             if (stringList[i].Length < stringList[j].Length)
             {
                 string tempString = stringList[i];
                 stringList[i] = stringList[j];
                 stringList[j] = tempString;
             }
         }
     }
 
     return stringList;
 }
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 phil_me_up · Dec 17, 2015 at 09:18 PM 0
Share

If you're deploying to iOS, be aware that LINQ isn't necessarily fully supported and you can run in to JIT errors. It's been a while since I tried though and many issues may have been fixed, but I avoid it for mobile deployment as a general rule. I've also read that it's slower perfor$$anonymous$$g than other methods (again, particularly on mobile) but I've not run any of my own tests to prove this.

avatar image Fattie phil_me_up · Dec 18, 2015 at 12:13 AM 0
Share

it's inconceivable performance would be an issue on this.

avatar image Fattie phil_me_up · Dec 18, 2015 at 12:17 AM 0
Share

you're right that it's still a mystery whether Unity have "sorted out the mystery iOS-Linq problems".

oh well, it's only a $$anonymous$$or platform iOS and Unity only costs a few thousand bucks a year, we can't expect them to sweat such details as whether the code compiles

avatar image Soraphis · Dec 17, 2015 at 09:33 PM 1
Share

no no no no no your second method is a no-go ... bubble sort -.-

 List<string> test = .... something .... ; 
 test.Sort(delegate(string x, string y){ return x.length - y.length });

im currently unsure if this would be the right order ... maybe this would be from longest to smallest word, if so: switch x.length - y.length to y.length - x.length

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

32 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

Related Questions

A node in a childnode? 1 Answer

Is it possible to name a list by a string variable? 2 Answers

Need help with switch case in order/timed 0 Answers

Update List<> in editor 0 Answers

How do I make a list of lists? 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