gpt4 book ai didi

javascript - AngularJS/JavaScript : Order array of objects digit by digit

转载 作者:行者123 更新时间:2023-12-01 04:02:40 27 4
gpt4 key购买 nike

我对大家有一些特殊的要求。我需要对以下对象数组进行自定义顺序/排序。我需要订购的数组可能类似于以下示例:

//array
$scope.myArray = [
{
orderId: "100"
}, {
orderId: "02"
}, {
orderId: "020"
}, {
orderId: "90"
}, {
orderId: "9"
},{
orderId: "52222"
}, {
orderId: "5223"
}, {
orderId: "522"
}, {
orderId: "800"
}, {
orderId: "080001"
}, {
orderId: "0009"
}
];

主要问题是,我必须通过属性 orderId 逐位排序 $scope.myArray 中的这些对象。我已经用 $scope.myTest = $filter('orderBy')($scope.myTest, 'orderId', false); 尝试过,但从侧面来看,这不会按数字对我的属性进行排序。

结果应该是这样的:

//abstract result order
0009, 02, 020, 080001, 100, 522, 52222, 5223, 800, 9, 90
//array ordered
$scope.myArray = [
{
orderId: "0009"
}, {
orderId: "02"
}, {
orderId: "020"
}, {
orderId: "080001"
}, {
orderId: "100"
},{
orderId: "522"
}, {
orderId: "52222"
}, {
orderId: "5223"
}, {
orderId: "800"
}, {
orderId: "9"
}, {
orderId: "90"
}
];

最佳答案

您可以采用 Javascript 形式进行构建,并按字符串形式的属性进行排序。

$scope = { myArray: [{ orderId: "100" }, { orderId: "02" }, { orderId: "020" }, { orderId: "90" }, { orderId: "9" }, { orderId: "52222" }, { orderId: "5223" }, { orderId: "522" }, { orderId: "800" }, { orderId: "080001" }, { orderId: "0009" }] };

$scope.myArray.sort(function (a, b) {
return a.orderId.localeCompare(b.orderId);
});

console.log($scope.myArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - AngularJS/JavaScript : Order array of objects digit by digit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42069666/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com