Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sendatsu_Yoshimitsu · Aug 07, 2014 at 10:07 AM · c#arraymathlogic

What's a more efficient way to sort an array into multiple groups?

I'm trying to work out an efficient way to implement population dynamics: if there are 9 office workers in a building I want to pass the list of workers into a function, and have it iterate through that list and assign one third of the total list to floors 1, one third to floor 2, and one third to floor 3. I currently do this with a series of for loops:

     for (int i = 0; i < numberOfWorkers/3; i++){
         workersArray[i].GetComponent<JobAssignment>().AssignedFloor = 1;
     }

     for (int i = numberOfWorkers/3; i < (numberOfWorkers/3)*2; i++){
         workersArray[i].GetComponent<JobAssignment>().AssignedFloor = 2;
     }

     for (int i = 0; i < numberOfWorkers; i++){
         workersArray[i].GetComponent<JobAssignment>().AssignedFloor = 3;
     }

This is extremely clunky, requires me to hard-code floor assignments, and will leave 10% of the total workers unassigned if I feed it a value that is not divisible by three. Is there a more efficient way to accomplish this that I'm missing?

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
0
Best Answer

Answer by vexe · Aug 07, 2014 at 10:28 AM

So it's not just repetitive but you're also overwriting the values you previously assigned since you're always start from 0.

Here's my 2 cents (works only with values divisible by 3):

         int[] values = { 1, 2, 3 };
         int oneThird = numberOfWorkers / 3;
         for (int i = 0, vIndex = -1; i < numberOfWorkers; i++)
         {
             if (i % oneThird == 0)
                 vIndex++;
             workers[i].floor = values[vIndex];
         }

and will leave 10% of the total workers unassigned if I feed it a value that is not divisible by three

Well since you want to assign each one third of your workers, you need a value divisible by three... You could maybe assign the left-overs some per-determined value, I'll edit my answer soon.

Edit: So here's two ways you could handle left-overs - the first is done in one loop but with more checks, the second is done via two loops but with less checks (same amount of iterations is done in both cases - Note that count is the number of workers):

         int[] values = { 1, 2, 3 };
         int leftoverValue = 2;  // assign any leftover workers the second floor
         int oneThird = count / 3;

         for (int i = 0, vIndex = -1; i < count; i++)
         {
             if (i % oneThird == 0)
                 vIndex++;
             workers[i].floor = vIndex < values.Length ? values[vIndex] : leftoverValue;
         }

Second:

         int[] values = { 1, 2, 3 };
         int leftoverValue = 2;
         int oneThird = count / 3;
         int threeThirds = oneThird * 3;

         for (int i = 0, vIndex = -1; i < threeThirds; i++)
         {
             if (i % oneThird == 0)
                 vIndex++;
             workers[i].floor = values[vIndex];
         }

         int leftovers = count % 3;
         for (int i = count - leftovers; i < count; i++)
         {
             workers[i].floor = leftoverValue;
         }



It'd be interesting if there's a LINQ solution to this, hmm....

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 Sendatsu_Yoshimitsu · Aug 07, 2014 at 11:04 AM 0
Share

So this works perfectly, I would've never thought of doing it that way- thank you so much! I've been banging my head against this for a week. :)

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

22 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

Related Questions

A problem with intersection detection 1 Answer

[C#] Buttons through an Array. 1 Answer

Clarification on how RangeAttribute works 1 Answer

Index out of Range Exception Error 2 Answers

Array index out of range error 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