0

I searched something similar accros the stackoverflow, but did not find nothing so specific. Could it be maybe that I ran to it but did not clearly understood that it is a similar case.

So I have an array of arrays which have two different type of values. For example, lets say that it looks like this:

var stuff = [
    ["C", 13.63, 15.12, 17.25, 18.87, 20.13],
    ["C++", 17.35, 19.13, 21.61, 23.53, 25.1],
    ["Django", 0.71, 0.77, 0.8, 0.87, 0.97],
    ["Git", 4.07, 4.31, 4.46, 4.79, 5.45],
    ["Java", 25.44, 26.97, 29.37, 31.99, 34.61]
]

So my first question is how to sort arrays by, for example, second value? In that case Java including array would be first and Django would be last.

Second question is: Is it possible to say "if array has value Git, put it as the first of arrays in stuff array?

dzordz
  • 2,277
  • 13
  • 51
  • 74
  • 1
    [Pass a callback to `.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). Basically you have to do the same as in [Sorting objects in an array by a field value in JavaScript](http://stackoverflow.com/q/1129216/218196), just with arrays instead of objects. If you want the array with "Git" on the top, it has to be "smaller" than any other value. – Felix Kling Sep 24 '14 at 20:16
  • Can you sort arrays with different types? – Sterling Archer Sep 24 '14 at 20:17
  • 1
    [Sort an array with arrays in it by string](http://stackoverflow.com/questions/5435228/sort-an-array-with-arrays-in-it-by-string) Sorting by number will be done the same way. I googled for *"javascript sort array of arrays site:stackoverflow.com"*, and it was the first result. –  Sep 24 '14 at 20:21
  • 1
    To move an array to the front, search for it with a loop, `.splice()` it out, and `.unshift()` it back in. –  Sep 24 '14 at 20:23
  • ok @squint, Thank you very much, Your first answer helped me and I managed to use it on my example, but I'm affraid that I'm not such expert to undestand fully your suggestion to my second example. :( – dzordz Sep 24 '14 at 20:35
  • @SterlingArcher, if it would lead me tho sort data to my needs than yes – dzordz Sep 24 '14 at 20:37
  • Here is a similar question to your second question: http://stackoverflow.com/q/6974069/218196 – Felix Kling Sep 24 '14 at 20:38
  • 1
    @dzordz: [`.splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) and [`.unshift()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift) are basic Array methods. It would be a good idea to read the MDN documentation of each and probably to review the other Array methods as they are commonly needed. If I understood you correctly, after the Array is sorted, you want to move the `Git` entry to the first position. –  Sep 24 '14 at 20:39

0 Answers0