AngularJS / JavaScript:逐位排序对象数组

时间:2017-02-06 14:03:27

标签: javascript angularjs math

我对你们有一些特别的要求。我需要为以下对象数组定制订单/排序。我需要订购的数组可能如下例所示:

//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"
    }
];

主要问题是,我必须通过数字$scope.myArrayorderId$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"
    }
];
routes.MapRoute("MyRouteName", // Route name
  "first-folder/second-folder/{param1}", // URL with parameters
new
{
    controller = "Redirect",
    action = "MyRedirectHandler",
    newRouteName = "mynewroute",
    folder1= "foo",
    folder2 = "bar" 
},  //param defaults
new {
    set = "option1|option2|option3"
} //param constraints
);

1 个答案:

答案 0 :(得分:2)

您可以使用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; }