# Sort 3 dimensional array

I need to sort a 3 dimensional array using the second element, where the second element is decimal value, with 2 decimal digits.  (miles).    I have this code from a previous; however, it seems to not work all the time.  And I do not understand the syntax or the return a[0] > b[0] ? -1 : 1; and  return a[1] > b[1] ? 1 : -1; statements and how
this accomplishes a sort.

So I have an array elements like this:

"Distance1", 7.4, 50
"Distance2", 5.7, 51
"Distance3", 12.45, 52
"Distance4", 2.64, 53

aryShowHomes.sort(
function (a, b) {
if (a[1] == b[1])
return a[0] > b[0] ? -1 : 1;
return a[1] > b[1] ? 1 : -1;
});
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Manager / Technology SpecialistCommented:
Basically your sort code tries to sort by the second property first.
If they are equal, it sorts by the string (the first property.
0
Author Commented:
ok.  I do not want that.   I dont want any sorting by any other elements/property.  The second element is mileage; if the mileage is the same, I dont want it to sort at all.
0
Author Commented:
Also, could you explain the syntax?  I do not understand.  I would prefer a function using "typical" sorting methods/syntax.
0
Manager / Technology SpecialistCommented:
That code is JavaScript and is using the native array sort function.
See the documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

So the function that you see there passed into the sort method will be called for each item in the array.
a represents the current item
b represents the next item in the array

if the result of this function is 1, means that a > b, 0 means that they are equal, -1 means a<b
0
Author Commented:
I wrote this bubble sort, (attached) which seems to work, where element [1] is the mileage.
0
Author Commented:
attachee
bubblesort.txt
0
Commented:
Also, could you explain the syntax?  I do not understand.  I would prefer a function using "typical" sorting methods/syntax.
As explained in the other question - the "syntax" is not the sort.

Here is how it works. The sort method loops through your array of items. In order to sort it needs to compare two items and find out which is bigger / smaller or which comes before the other. If the array is a simple array of strings and you don't supply a compare function then it uses its own string compare function to determine which element comes first.

In your case you are supplying it with a custom compare function. Each time the .sort method needs to find out which of two elements comes before the other it calls your custom method passing as parameters the two elements it wants to compare. All your method needs to do is determine (by whatever means you choose) which of the elements preceeds the other and returns
-1 if a < b (i.e a must come before b)
0 if they are equal
1 if a > b (i.e. b must come before a)

The function you are using does exactly that - although you have omitted the equality comparison
``````function (a, b) {
if (a[1] == b[1])
return a[0] > b[0] ? -1 : 1;
return a[1] > b[1] ? 1 : -1;
}
``````
This can be rewritten as follows
``````function (a,b) {
if (a[1] == b[1]) {
if (a[0] > b[0]) return -1;
else return 1;
}
if (a[1] > b[1]) return 1;
return -1;
}
``````
All this code is doing is working out which of the two values comes first and returns a -1, 0 or 1 to tell the .sort method what the result of the comparison is.

Obviously the above example is based on your posted code and that is clearly not doing what you want - the question is - are you only sorting on the second column - do you not care about the other two values - if so you could then do this
``````function(a,b) {
if (a[1] == b[1]) return 0;
return a[1] < b[1] ? -1 : 1;
}
``````
If you want to sort descending then
``````function(a,b) {
if (a[1] == b[1]) return 0;
return a[1] > b[1] ? -1 : 1;
}
``````
If you want to sort by some other additional criteria then let us know what that is so we can assist.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
I need to sort a 3 dimensional array
Just to clarify - based on the data posted it appears you have a 2D array i.e an array of arrays rather than an array of arrays of arrays. Is that correct. Does your data look like this?

``````var data = [
["Distance1", 7.4, 50],
["Distance2", 5.7, 51],
["Distance3", 12.45, 52],
["Distance4", 2.64, 53],
]
``````

Sample code that demonstrates the concept
``````<script type="text/javascript">
var data = [
["Distance1", 7.4, 50],
["Distance2", 5.7, 51],
["Distance3", 12.45, 52],
["Distance4", 2.64, 53],
];

x = data.sort(function (a,b) {
if (a[1] === b[1]) return 0;
if (a[1] > b[1]) return -1;
return 1;
});

console.log(x);
</script>
``````
Output
``````0  ["Distance3", 12.45, 52]
1  ["Distance1", 7.4, 50]
2  ["Distance2", 5.7, 51]
3  ["Distance4", 2.64, 53]
``````
0
Author Commented:
you are correct.  It's an array of arrays.  My mistake.

0
Author Commented:
Thanks for the help
0
Commented:
You are welcome.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.