Hi, I know how to sort numbers with javascript: var a = 1 var b = 2 var c = 3 document.write(Math.min(a,b,c)) Code (markup): The Output: 1 Code (markup): I would like to have as output the variable which present the lowest value. So, in this case, my output should be 'a' because a has the lowest value. Can someone help me? Thx in advance and Merry Christmas
Why do you need the name of the variable? Do you need the variable further down in the script? Seems strange to me?!
Yes I need it further down. I am creating an algorithm (shortest path), when the value of a point is lower than the values of the other points, you have to add that point to a list so I can see which points are left, the algorithm is finished when all points are in the list.
try this code from w3schools <script type="text/javascript">function sortNumber(a,b) { return a - b; }var arr = new Array(6); arr[0] = "10"; arr[1] = "5"; arr[2] = "40"; arr[3] = "25"; arr[4] = "1000"; arr[5] = "1";document.write(arr + "<br />"); document.write(arr.sort(sortNumber));</script> Code (markup):
The scripts you gave me are to sort the values of the array, I needed the names of the Array. I just found a good script: <script language="javascript"> var myArray = Array(); myArray["A"] = 3; myArray["B"] = 2; myArray["C"] = 5; var lowest = ""; for (key in myArray) { if (lowest == "" || myArray[key] < myArray[lowest]) { lowest = key; } } document.write(lowest) </script> Code (markup): Thx anyway for your help