Quantcast
Channel: Latest Questions by Flynn
Viewing all articles
Browse latest Browse all 48

Unity JS array custom sort

$
0
0
Hello :) I've been all over Unity answers tyring to answer this one myself, but nothing I come across appears to be working... The following is what I'd like to do: var arr = [1, 2, 3, 4, 6, 5]; arr.Sort(sortFunc); function sortFunc(a, b) { return b-a; } Debug.Log(arr);//[6, 5, 4, 3, 2, 1] However, the actual syntax for applying a custom sort function to a JS generic array is proving difficult to find. Anyone willing to offer some help? (P.S. this is for Unity 2, NOT 3 (compatability reasons, hard to explain)) #EDIT 1: The array I am sorting is constructed like so: var arr = ["a", "b", "c", "d"]; //Next, I run arr through a function that returns: var arr = [["a", 1], ["b", 2], ["c", 3], ["d", 4]]; //Finally, try to sort it as shown above. My sorter is arranged like so: function SortIEByType(a : Array, b : Array) { var ra = parseInt(a[0][1]); var rb = parseInt(b[0][1]); return ra-rb; } I have specified the a and b as Array as you did for integers... No effect :( I also had a : int appended to my method, but that did not work either. Any ideas?

Viewing all articles
Browse latest Browse all 48

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>