$.ajax({
url: '/seatlt',
type: 'POST',
dataType: 'JSON',
data: data,
async: true,
crossDomain: true,
success: function(res) {
$.each(res, function(i, v) {
$.each(v, function(j, w) {
var ggg = w.name;
vvv.push(ggg);
})
})
}
})
In console.log(vvv) I got the output as:
["22", "A", "23", "B", "24", "C", "25", "D", "26", "E", "27", "F", "28", "29", "30", "31", "10", "32", "11", "33", "12", "34", "13", "35", "14", "15", "16", "17", "18", "19", "1", "2", "3", "4", "5", "6", "7", "8", "9", "20", "21"].
But I want the output as:
["A", "B","C","D","E", "F","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"].
I'm writing blind, but this should do the trick.
res.sort(function(a, b) {
var AisNumber = a % 1 === 0;
var BisNumber = b % 1 === 0;
if (AisNumber && BisNumber) {
return Number(a) - Number(b);
} else if (AisNumber && !BisNumber) {
return 1;
} else if (!AisNumber && BisNumber) {
return -1;
} else {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
});
sort the array?
vvv.sort();
That works for me. But and you can sort the numbers as well, eg:
var arr = ["A", "9", "B", "1", "8", "9", "C", "10", "11", "17", "20", "21", "30", "31", "35"];
for (var i = 0; i < arr.length; i++) {
try{
arr[i] = eval(arr[i]);
}
catch (err) {
//ignore
}
}
arr.sort();
alert(arr);
You could check for number and sort this values at the bottom and the rest to top.
return isNumber(a) - isNumber(b) || a - b || a > b || -(a < b);
// ^^^^^^^^^^^^^^^^^^^^^^^^^ check group
// ^^^^^ order numbers
// ^^^^^^^^^^^^^^^^^ order strings
var array = ["22", "A", "23", "B", "24", "C", "25", "D", "26", "E", "27", "F", "28", "29", "30", "31", "10", "32", "11", "33", "12", "34", "13", "35", "14", "15", "16", "17", "18", "19", "1", "2", "3", "4", "5", "6", "7", "8", "9", "20", "21"]; array.sort(function (a, b) { function isNumber(v) { return (+v).toString() === v; } return isNumber(a) - isNumber(b) || a - b || a > b || -(a < b); }); console.log(array)
.as-console-wrapper { max-height: 100% !important; top: 0; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.