-1

ARRAYS:Accessing objects and sorting in JavaScript with multiple conditions. HI i am very new to javascript so it might seem very basic question. i have to sort according to the status field..

var issues = cf.getEventMessage(reply); //issues has all the data
var assigneeTasks = {};
for(var i=0;i<issues.length ;i++){
          var record = issues[i];
          if(assigneeTasks[record.assigneemail] == undefined){
            assigneeTasks[record.assigneemail] = [];
          }
            assigneeTasks[record.assigneemail].push(record); //sorted according to assigneemail
        }

now assigneeTasks has

{"dev@vtu.com":
[
{"id":"T-728","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"Open"},
{"id":"T-727","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-726","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-10-04","status":"Open"},
{"id":"T-679","assignedTo":"devt","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"Under Review"},
{"id":"T-645","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-27","status":"In Progress"}
],


"paul@vtu.com":
[
{"id":"T-728","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"Open"},
{"id":"T-727","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-726","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-10-04","status":"Open"},
{"id":"T-679","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"Under Review"},
{"id":"T-645","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-27","status":"In Progress"}
]
}

What I want is

{"dev@vtu.com":
[
{"id":"T-728","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"Open"},
{"id":"T-726","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-10-04","status":"Open"},
{"id":"T-727","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-645","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-27","status":"In Progress"}
{"id":"T-679","assignedTo":"devt","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"Under Review"},
],

"paul@vtu.com":
[
{"id":"T-728","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"Open"},
{"id":"T-726","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-10-04","status":"Open"},
{"id":"T-727","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-645","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-27","status":"In Progress"}
{"id":"T-679","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"Under Review"},
]
}
  • Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Sep 29 '17 at 10:45
  • When you were asking your question, there was a big orange **How to Format** box to the right of the text area with useful information in it. There was also an entire toolbar of formatting aids. And a **[?]** button giving formatting help. *And* a preview area located between the text area and the Post Your Question button (so that you'd have to scroll past it to find the button, to encourage you to look at it) showing what your post would look like when posted. Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. – T.J. Crowder Sep 29 '17 at 10:45
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Sep 29 '17 at 10:46

1 Answers1

0

to sort array in object values you can use this code, if you want to sort based on number in id

function num(string) {
  return parseInt(id.replace(/[^0-9]+/g, ''));
}

function compare(a, b) {
  a = num(a.id);
  b = num(b.id);
  if (a < b) {
    return -1;
  }
  if (a > b) {
    return 1;
  }
  return 0;
}


Object.keys(obj).forEach(function(key) {
   output[key].sort(compare);
});

if you want to also sort based on date you need to incorporate answer to this question Sort Javascript Object Array By Date

jcubic
  • 51,975
  • 42
  • 183
  • 323
  • I want to sort based on the status field in each set – user8583902 Oct 03 '17 at 05:35
  • @user8583902 [Sort array of objects by string property value in JavaScript](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) and if you want specific order of statuses you can create object with numbers as values for specific status: `statuses = {"Open": 1, "In Progress": 2, "Under Review": 3}` and use `a = statuses[a.status]; b = statuses[b.status];` in compare function – jcubic Oct 03 '17 at 08:14