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 Le-Capitaine · Jul 12, 2018 at 12:09 PM · listsorting

Is List.Sort() reliable when some values are equal?

alt text

I have a list of missions sorted by price; I'm using their index in the list to determine in PlayerPrefs whether they've been cleared. The catch is that some missions have the same price.

What's the secondary means of sorting in this case? Will it reliably output the same order across launches and platforms?

m.png (180.0 kB)
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

1 Reply

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

Answer by JVene · Jul 12, 2018 at 05:44 PM

The documentation from Microsoft on the C# language and libraries states the sort is unstable.

That's CompSci speak for "no" to your question.

You can make it stable relative to your concerns by providing your own Comparison, then perform the comparison on more than one element of data to such an extent that no two items will be identical.

That requires that there be a stable, unique identifier. Only you would know what qualifies.

Sometimes the secondary (and occasionally several elements) are required. If you select elements that change over time, though, that would change the sorting order. Your stated requirement suggests you must choose elements that don't change. Position, for example, is a bad choice for this.

If nothing else is available, you would have to consider creating a "serial number" for your data, so that every object gets a unique numeric (presumably) identifier, used only for that situation that "breaks the tie" for prices.

The comparison function would be slightly different than a default Comparison (which works on one field of data), and would look something like:

 if ( x.price < y.price ) return -1;
 if ( x.price > y.price ) return 1;
 if ( x.serial < y.serial ) return -1;
 if ( x.serial > y.serial ) return 1;
 return 0;

This form can be extended to any number of "subfield" comparisons for sorting, in general usage. Some comparison functions in various languages only return greater or less than (relying on, say, not less than to mean greater or equal), which implies weak ordering. This interface requires strong ordering (separating equal from less or greater), but doesn't imply stability. You're "artificially" implementing stability relative to your data because you know there will be no two serial numbers that match in your data.

Comment
Add comment · Show 1 · 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 Le-Capitaine · Jul 13, 2018 at 06:08 PM 1
Share

That's my answer. I know what I'll be using.

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

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

Orderrring a List Ascending or Descending C# Advanced 2 Answers

Sorting a list by three parameters (C#) 1 Answer

sort list of object and if items are the same use another vaue 2 Answers

sorting list by name 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