0

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);


  }
4
  • ok. but where is the code to sort it? Commented Feb 26, 2018 at 3:22
  • normally I should get this sorted already but something is happening and the sort is changing the order. Commented Feb 26, 2018 at 3:23
  • take a look at this link... Refresh it and you'll see that changes thedev.koalito.com/newPlanning2/fullView.php Commented Feb 26, 2018 at 3:24
  • I'm not seeing the symptoms you are describing in the link you posted. Commented Feb 26, 2018 at 4:19

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hello. Thank you for your help, I tried this also and it doesn't seems to fix the problem. I always get the same result.
0

In your sort() it's missing property dayId, try this

taskPerDay.sort(function(a,b){ return a.dayId - b.dayId; })

2 Comments

I tried this, and this won't work at all. I don't even know why
Maybe it's not an array, but an object instead?
0

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

1 Comment

Your coul write this inside sort function ..return a.dayId >b.dayId?1:-1
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.