Reputation: 181
EDIT: I FIXED IT
Well I fixed it, I don't know how and what but all I did is I moved the init of var personTaskMonthly=[]; from
$http.get().then(function(data){
var personTaskMonthly=[];})
to before the $http starts like this
var personTaskMonthly=[];
$http.get().then(function(data){})
Thank you all for helping me out on this problem.
EDIT:
I found out where the problem is, but I don't know how to fix that:
Everything seems to be fine till I execute the $http.get().then(function(data){
/****** HERE I'm losing the correct order ****/});
Its a complex code where I have 3 nested angular foreach functions.
And finally, somehow I get the desired results.
But the result is not sorted correctly and I don't know why.
This is the result:
{dayId: 1, dayName: "Jeudi", personTaskMonthly: Array(2), $$hashKey: "object:79"}
{dayId: 3, dayName: "Samedi", personTaskMonthly: Array(0), $$hashKey: "object:82"}
{dayId: 2, dayName: "Vendredi", personTaskMonthly: Array(0), $$hashKey: "object:85"}
{dayId: 5, dayName: "Lundi", personTaskMonthly: Array(0), $$hashKey: "object:88"}
{dayId: 6, dayName: "Mardi", personTaskMonthly: Array(0), $$hashKey: "object:91"}
{dayId: 4, dayName: "Dimanche", personTaskMonthly: Array(0), $$hashKey: "object:94"}
{dayId: 10, dayName: "Samedi", personTaskMonthly: Array(0), $$hashKey: "object:97"}
{dayId: 7, dayName: "Mercredi", personTaskMonthly: Array(0), $$hashKey: "object:100"}
{dayId: 12, dayName: "Lundi", personTaskMonthly: Array(0), $$hashKey: "object:103"}
{dayId: 8, dayName: "Jeudi", personTaskMonthly: Array(0), $$hashKey: "object:106"}
{dayId: 11, dayName: "Dimanche", personTaskMonthly: Array(0), $$hashKey: "object:109"}
{dayId: 9, dayName: "Vendredi", personTaskMonthly: Array(0), $$hashKey: "object:112"}
{dayId: 18, dayName: "Dimanche", personTaskMonthly: Array(0), $$hashKey: "object:115"}
{dayId: 13, dayName: "Mardi", personTaskMonthly: Array(0), $$hashKey: "object:118"}
{dayId: 15, dayName: "Jeudi", personTaskMonthly: Array(0), $$hashKey: "object:121"}
And My expected result is, something like this:
{dayId: 1, dayName: "Jeudi", personTaskMonthly: Array(2), $$hashKey: "object:79"}
{dayId: 2, dayName: "Vendredi", personTaskMonthly: Array(0), $$hashKey: "object:85"}
{dayId: 3, dayName: "Samedi", personTaskMonthly: Array(0), $$hashKey: "object:82"}
{dayId: 4, dayName: "Dimanche", personTaskMonthly: Array(0), $$hashKey: "object:94"}
{dayId: 5, dayName: "Lundi", personTaskMonthly: Array(0), $$hashKey: "object:88"}
{dayId: 6, dayName: "Mardi", personTaskMonthly: Array(0), $$hashKey: "object:91"}
{dayId: 7, dayName: "Mercredi", personTaskMonthly: Array(0), $$hashKey: "object:100"}
{dayId: 8, dayName: "Jeudi", personTaskMonthly: Array(0), $$hashKey: "object:106"}
{dayId: 9, dayName: "Vendredi", personTaskMonthly: Array(0), $$hashKey: "object:112"}
{dayId: 10, dayName: "Samedi", personTaskMonthly: Array(0), $$hashKey: "object:97"}
{dayId: 11, dayName: "Dimanche", personTaskMonthly: Array(0), $$hashKey: "object:109"}
{dayId: 12, dayName: "Lundi", personTaskMonthly: Array(0), $$hashKey: "object:103"}
{dayId: 13, dayName: "Mardi", personTaskMonthly: Array(0), $$hashKey: "object:118"}
....
Can you help me please how do I sort this json result in order to achieve the desired thing?
this is my angularjs code:
function getMonthlyTask(daysInterv){
/*
var sDateHour = $scope.dayInterval[0].dsUnix;
var eDateHour = $scope.dayInterval[$scope.dayInterval.length-1].deUnix; */
var taskPerDay=[];
var i=0;
var counter = [];
daysInterv.forEach(function(item, ind){
var sDateHour = item.dsUnix;
var eDateHour = item.deUnix;
var dayName = item.dayName;
var dId = item.id;
$http.get('link')
.then(function(data){
var datas = data.data;
var personTaskMonthly=[];
datas.forEach(function(it, ind){
var idProfile = it.id_profile;
var idGroupe = it.id_groupe;
if (it.tasks){
var countPT = {};
var count;
it.tasks.forEach(function(k,v){
if(k.comptable==1){
if(!countPT[k.id_profile]){
count=1;
countPT[k.id_profile]=count;
}else{
count+=1;
countPT[k.id_profile]=count;
}
}
})
personTaskMonthly.push({'id_profile':it.id_profile, 'groupe':it.groupe,countPT});
}
})
taskPerDay.push({'dayId':dId,'dayName':dayName,personTaskMonthly});
//$scope.personTaskMonthly=personTaskMonthly;
});//ending then
})
console.log(taskPerDay);
var sorted = taskPerDay.sort(function(a,b){return a-b});
$scope.taskPerDay = sorted;
console.log(sorted);
}
Upvotes: 0
Views: 136
Reputation: 181
EDIT: I FIXED IT
Well I fixed it, I don't know how and what but all I did is I moved the init of var personTaskMonthly=[]; from
$http.get().then(function(data){
var personTaskMonthly=[];})
to before the $http starts like this
var personTaskMonthly=[];
$http.get().then(function(data){})
Thank you all for helping me out on this problem.
Upvotes: 0
Reputation: 161
Your sort function is using the whole object to compare you need to use as like
taskPerDay.sort(function(a,b){ return a.dayId - b.dayId; })
but alternatively you can use an angular 2/4/5 pipe to sort them in HTML code itself if you don't need the sorted array in javascript, there are lot of third party pipes available eg ngx-order-pipe. If you are on Angular 1.x you can use the default orderBy filter to do this for you
Upvotes: 1
Reputation: 6548
you can try this.
var items = [{dayId: 1, dayName: "Jeudi", personTaskMonthly: Array(2), $$hashKey: "object:79"},
{dayId: 3, dayName: "Samedi", personTaskMonthly: Array(0), $$hashKey: "object:82"},
{dayId: 2, dayName: "Vendredi", personTaskMonthly: Array(0), $$hashKey: "object:85"},
{dayId: 5, dayName: "Lundi", personTaskMonthly: Array(0), $$hashKey: "object:88"},
{dayId: 6, dayName: "Mardi", personTaskMonthly: Array(0), $$hashKey: "object:91"},
{dayId: 4, dayName: "Dimanche", personTaskMonthly: Array(0), $$hashKey: "object:94"},
{dayId: 10, dayName: "Samedi", personTaskMonthly: Array(0), $$hashKey: "object:97"},
{dayId: 7, dayName: "Mercredi", personTaskMonthly: Array(0), $$hashKey: "object:100"},
{dayId: 12, dayName: "Lundi", personTaskMonthly: Array(0), $$hashKey: "object:103"},
{dayId: 8, dayName: "Jeudi", personTaskMonthly: Array(0), $$hashKey: "object:106"},
{dayId: 11, dayName: "Dimanche", personTaskMonthly: Array(0), $$hashKey: "object:109"},
{dayId: 9, dayName: "Vendredi", personTaskMonthly: Array(0), $$hashKey: "object:112"},
{dayId: 18, dayName: "Dimanche", personTaskMonthly: Array(0), $$hashKey: "object:115"},
{dayId: 13, dayName: "Mardi", personTaskMonthly: Array(0), $$hashKey: "object:118"},
{dayId: 15, dayName: "Jeudi", personTaskMonthly: Array(0), $$hashKey: "object:121"}]
var sortedArray = items.sort(function(a,b){
return a.dayId >b.dayId?1:a.dayId <b.dayId?-1:0
})
console.log(sortedArray);
updated and working fiddle here
Upvotes: 0
Reputation: 885
In your sort() it's missing property dayId, try this
taskPerDay.sort(function(a,b){ return a.dayId - b.dayId; })
Upvotes: 0