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 /
  • Help Room /
avatar image
0
Question by Flamacore · Dec 17, 2015 at 08:36 PM · sortinggenericsmultidimensional array

How do i sort multidimensional javascript array?

Hey there everyone. First, i know this has been answered before but only partly. So here goes my question, i'll try to be as quick as possible :)

Say i have 8 dynamically constructed string arrays using a way described below:

 var array1 = decryptedData[0].ToString().Split(";"[0]); //decryptedData is a variable also filled dynamically
 var array2 = decryptedData[1].ToString().Split(";"[0]);
 var array3 = decryptedData[2].ToString().Split(";"[0]);
 var array4 = decryptedData[3].ToString().Split(";"[0]);
 var array5 = decryptedData[4].ToString().Split(";"[0]);
 var array6 = decryptedData[5].ToString().Split(";"[0]);
 var array7 = decryptedData[6].ToString().Split(";"[0]);
 var array8 = decryptedData[7].ToString().Split(";"[0]);


Then, to be able to control all these data easliy and specifically to sort them properly like you would do in a data table; I create a multidimensional 2d array from them using this:

 var BIGARRAY = [array1,array2,array3,array4,array5,array6,array7,array8];

So the question is; how do i sort all of them according to e.g. array2 ? All arrays are strings but some of them are actually numbers as strings if that helps.

Being not even sure if that's a correct syntax, tried this with failure;

 System.Array.Sort(BIGARRAY, mySorting);
 function mySorting(a,b) {
 var str1 = a[0][0];
 var str2 = b[0][0];
 var n = str1.localeCompare(str2);
 return n;
 }





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

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

do i get you correct: you want to sort array2 alphabetically, and every other array according to this?

if so: https://jsfiddle.net/oopqmzv8/

well its plain javascript but it should work to get the concept.

imagine the sub arrays as columns in a table, rearranging them so that the X't (0-indexed) column is sorted:

first i change the 2d array so, that i have an array of rows (instead columns) and then i sort them for the rows X't entry

at last i change the array again, so that i get columns again and return it

Edit:

alternatively (note: its C# code)

  private void SortTable(string[][] table, int column) {
         // for each column in table:
         for (int i = 0; i < table.Length; i++) {
             Array.Sort(table[i], (a, b) => String.Compare(table[column][Array.IndexOf(table[i], a)], 
                                                           table[column][Array.IndexOf(table[i], b)], StringComparison.Ordinal));
         }
     }

its basicly the same as my fiddle but it should be faster and less to write

Comment
Add comment · Show 6 · 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 Flamacore · Dec 17, 2015 at 09:29 PM 0
Share

Wow thanks for the immediate answer :) Yes you are correct in what i need but unfortunately the link only points to an empty jsfiddle page :/ Perhaps you just missed it? :)

avatar image Soraphis Flamacore · Dec 17, 2015 at 09:36 PM 0
Share

ahh your right :D i did not clicked on Save XD link is now updated: https://jsfiddle.net/oopqmzv8/

avatar image Flamacore Soraphis · Dec 17, 2015 at 09:44 PM 0
Share

Hmm.. Well i sort of get what you're getting at and that looks pretty neat actually, thank you so much for trying to help :) But probably specific to Unity, i am getting and Index is Out of Range error.. Any idea why that might be?

Show more comments
Show more comments

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

33 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

Related Questions

Sorting an Array of Arrays for game Object 2 Answers

How to sort List of Lists by a certain Value (C#) 1 Answer

help please. I wrote a mergesort algorithm that does nothing. 0 Answers

How to make Unity singleton base class to support generics? 1 Answer

Help to Add some Sort and Binary Search in the code. 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