JavaScript数组排序在默认Android浏览器中不起作用
我对下面列出的函数有问题。在我的电脑上,这段代码工作得很好,在我的手机上的Chrome上也是如此。然而,当我在默认的Android浏览器中运行这段代码时,结果是排序数组的前4个元素不是它们应该的样子 元素1:0 要素2:128 要素3:256 要素4:384 现在我想知道为什么会是这种情况,直到数组被排序为止,收集的值都是好的,之后,它们是不正确的 根据此链接,它应该与默认的Android浏览器兼容:JavaScript数组排序在默认Android浏览器中不起作用,javascript,sorting,android-browser,Javascript,Sorting,Android Browser,我对下面列出的函数有问题。在我的电脑上,这段代码工作得很好,在我的手机上的Chrome上也是如此。然而,当我在默认的Android浏览器中运行这段代码时,结果是排序数组的前4个元素不是它们应该的样子 元素1:0 要素2:128 要素3:256 要素4:384 现在我想知道为什么会是这种情况,直到数组被排序为止,收集的值都是好的,之后,它们是不正确的 根据此链接,它应该与默认的Android浏览器兼容: 如果它在所有其他情况下都有效,请尝试将返回a.value-b.value;排成一行。更好的是
如果它在所有其他情况下都有效,请尝试将返回a.value-b.value;排成一行。更好的是缩小。不幸的是,这并没有解决问题。
function sendWeekProgram() {
//Get all the info to send
var weekProgramData;
//Check if the week program is enabled
if($("#vacationMode").prop('checked') === false) {
weekProgramData = '<week_program state="on">';
}else {
weekProgramData = '<week_program state="off">';
}
//For each day get the switches
for(i = 0; i < 7; i++) {
//Set the day name
switch(i) {
case 0:
dayName = "Monday";
break;
case 1:
dayName = "Tuesday";
break;
case 2:
dayName = "Wednesday";
break;
case 3:
dayName = "Thursday";
break;
case 4:
dayName = "Friday";
break;
case 5:
dayName = "Saturday";
break;
case 6:
dayName = "Sunday";
break;
}
//Add the day to the query
weekProgramData = weekProgramData + '<day name="' + dayName + '">';
//Create an array
var arraySwitches = [];
//Put all day switches in an array
for(j = 0; j < 5; j++) {
//Check if the switch is on or off
if(($("#daySwitch" + dayName + (j + 1)).hasClass("dayOverViewTextActive") === false) || ($("#nightSwitch" + dayName + (j + 1)).hasClass("dayOverViewTextActive") === false)) {
arraySwitches[j] = {type: 'day', state: 'off', value: parseInt($("#daySwitch" + dayName + (j + 1)).text().replace(":", ""))};
}else {
arraySwitches[j] = {type: 'day', state: 'on', value: parseInt($("#daySwitch" + dayName + (j + 1)).text().replace(":", ""))};
}
}
//Put all night switches in an array
for(j = 5; j < 10; j++) {
//Check if the switch is on or off
if(($("#daySwitch" + dayName + (j-4)).hasClass("dayOverViewTextActive") === false) || ($("#nightSwitch" + dayName + (j-4)).hasClass("dayOverViewTextActive") === false)) {
arraySwitches[j] = {type: 'night', state: 'off', value: parseInt($("#nightSwitch" + dayName + (j-4)).text().replace(":", ""))};
}else {
arraySwitches[j] = {type: 'night', state: 'on', value: parseInt($("#nightSwitch" + dayName + (j-4)).text().replace(":", ""))};
}
}
//Sort the array
arraySwitches.sort(function(a, b) {
return a.value
- b.value;
});
//For each element in the array, add it to the weekProgramData
for(k = 0; k < 10; k++) {
//Add leading zeroes
arrayValue = arraySwitches[k].value.toString();
while(arrayValue.length < 4) {
arrayValue = "0" + arrayValue;
}
//First convert the integer back to the time
time = arrayValue.substr(0,2) + ':' + arrayValue.substr(2);
//Add value to weekprogram
weekProgramData = weekProgramData + '<switch type="' + arraySwitches[k].type + '" state="' + arraySwitches[k].state + '">' + time + '</switch>';
}
//Add the closing tag of the day
weekProgramData = weekProgramData + '</day>';
}
//Add the closing tag of the week program
weekProgramData = weekProgramData + '</week_program>';
//Send the info
putInfo("weekProgram", weekProgramData);
}