采用了棱角分明的js排序JSON阵列的JSON数组 [英] sorting json arrays in json array using angular js
问题描述
我JSON数组的格式如下,
$ scope.data = {[
价值观:[[2日,103.89],[NextDay,107.41],[地,428.75],
钥匙:联邦快递
},{
价值观:[[地,117.8],[NextDay,10],[2日,15],
钥匙:UPS
}]
我需要它,以以下格式进行排序:
$ scope.data = {[
价值观:[[2日,103.89],[NextDay,107.41],[地,428.75],
钥匙:联邦快递
},{
价值观:[[2日,15],[NextDay,10],[地,117.8],
钥匙:UPS
}]
我能如何使用Angularjs呢?
为此,我需要应用类似的排序,但在这里我有时间(长格式)类似的数据集,而不是字符串。
$ scope.data1 = {[
价值观:[13590.72亿,103.89],[1365116400000,107.41],[1357516800000,428.75],
钥匙:联邦快递
},{
价值观:[[1357516800000,117.8],[13590.72亿,100],[1365116400000,15],
钥匙:UPS
}];
要被格式化为
$ scope.data1 = {[
价值观:[13590.72亿,103.89],[1365116400000,107.41],[1357516800000,428.75],
钥匙:联邦快递
},{
价值观:[13590.72亿,100],[1365116400000,15],[1357516800000,117.8],],
钥匙:UPS
}];
自然排序,可在这样的js应用。因为你的数组中的字符串包含数字是必需的自然排序。
函数strcmp(A,B){
返回> B' 1:&下; B' -1:0;
}功能natcmp(A,B){
变种X = [],Y = []; A [0] .replace(/(\\ d +)|(\\ D +)/克,函数($ 0,$ 1,$ 2){x.push([$ 1 || 0,$ 2])})
B [0] .replace(/(\\ d +)|(\\ D +)/克,函数($ 0,$ 1,$ 2){y.push([$ 1 || 0,$ 2])}) 而(x.length&安培;&安培; y.length){
变种XX = x.shift();
变种YY = y.shift();
VARン=(YY [0] -XX [0])||的strcmp(YY [1]中,xx [1]);
如果(NN)NN收益;
} 如果(x.length)返回-1;
如果(y.length)返回+1; 返回0;
}
应用使用JavaScript排序功能的阵列中的排序如下图所示。
$ scope.data = $ scope.data.map(功能(D){d.values = d.values.sort(natcmp);返回D组;});
不需要用于所述第二数据集自然排序。要按照降序排序的数组,试试这个。
$ scope.data1 = $ scope.data1.map(功能(D){
d.values = d.values.sort(功能(A,B){
返回新的日期(B [0]) - 新的日期(A [0])
});
返回D组;
});
I have json array in the following format,
$scope.data = [{
"values" : [["2 Day", 103.89], ["NextDay", 107.41], ["Ground", 428.75]],
"key" : "FedEx"
}, {
"values" : [["Ground", 117.8], ["NextDay", 10], ["2 Day", 15]],
"key" : "UPS"
}]
I need to sort it in to the following format :
$scope.data = [{
"values" : [["2 Day", 103.89], ["NextDay", 107.41], ["Ground", 428.75]],
"key" : "FedEx"
}, {
"values" : [["2 Day", 15], ["NextDay", 10], ["Ground", 117.8]],
"key" : "UPS"
}]
How can I do it using Angularjs?
A similar data set for which I want similar sorting to be applied, but here I have time (in long format) instead strings.
$scope.data1 = [{
"values" : [[1359072000000, 103.89], [1365116400000, 107.41], [1357516800000, 428.75]],
"key" : "FedEx"
}, {
"values" : [[1357516800000, 117.8], [1359072000000, 100], [1365116400000, 15]],
"key" : "UPS"
}];
To be formatted as
$scope.data1 = [{
"values" : [[1359072000000, 103.89], [1365116400000, 107.41], [1357516800000, 428.75]],
"key" : "FedEx"
}, {
"values" : [[1359072000000, 100],[1365116400000, 15], [1357516800000, 117.8], ],
"key" : "UPS"
}];
Natural sorting can be applied in js like this. Natural sorting is required since strings in your array contains numbers.
function strcmp(a, b) {
return a > b ? 1 : a < b ? -1 : 0;
}
function natcmp(a, b) {
var x = [], y = [];
a[0].replace(/(\d+)|(\D+)/g, function($0, $1, $2) { x.push([$1 || 0, $2]) })
b[0].replace(/(\d+)|(\D+)/g, function($0, $1, $2) { y.push([$1 || 0, $2]) })
while(x.length && y.length) {
var xx = x.shift();
var yy = y.shift();
var nn = (yy[0]-xx[0]) || strcmp(yy[1],xx[1]);
if(nn) return nn;
}
if(x.length) return -1;
if(y.length) return +1;
return 0;
}
Apply sorting in your array using javascript sort function as shown below.
$scope.data = $scope.data.map(function(d){ d.values = d.values.sort(natcmp); return d; });
Natural sorting is not needed for the second dataset. To sort the array in descending order by time, try this.
$scope.data1 = $scope.data1.map(function(d) {
d.values = d.values.sort(function(a, b) {
return new Date(b[0]) - new Date(a[0])
});
return d;
});
这篇关于采用了棱角分明的js排序JSON阵列的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!