首页 > 代码库 > 数组对象用ID排序

数组对象用ID排序

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>

<body>
11111
</body>

</html>
<script>
var arr = [
{sizeId:"001",sizeName:"M"},
{sizeId:"002",sizeName:"L"},
{sizeId:"004",sizeName:"S"},
{sizeId:"008",sizeName:"SM"},
{sizeId:"006",sizeName:"SX"}
];

function compare(prop) {
return function (obj1,obj2) {
var val1 = obj1[prop];
var val2 = obj2[prop];
return val1 -val2;
}
}
var sortObj = arr.sort(compare("sizeId"));
console.log(sortObj);
</script>

数组对象用ID排序