Issue
Using angular, I'm pulling data from my API. I'm unsure about two things if someone could give me insight:
Should I be sorting the array on the
Observableor theSubscriberand why?I'm unsure how to sort my array of data based off if the values are true or false using the
sort()function.
Here is what my object looks like, if any of the values after hostname come back false, or if the date on UpdatedDate is over 4 weeks, I need that object to show first. If everything is true, but the date is > 2 weeks and also < 4 weeks, I need it to come second. And if the object is completely true with a date that is less than 2 weeks old, I want it to come last. There will be about 500 of these Ipcidata objects coming through.
export interface Ipcidata {
id: Int16Array;
hostname: string;
AMStatus: boolean;
BLStatus: boolean;
FirewallRullStatus: boolean;
FirewallContentStatus: boolean;
SCCMStatus: boolean;
MSBaselineStatus: boolean;
UpdatedDate: Date;
USBStatus: boolean;
}
Here's my observable, with a sort function ready
getPciInfo(): Observable <Ipcidata[]> {
return this.httpClient.get<Ipcidata[]>('http://localhost:499/api/PCImachines').pipe(
map(results => results.sort()
)
);
}
Lastly, here's some logic I wrote that does exactly what I need to check for:
sortObjects(){
const latest_date = this.datepipe.transform(this.systemInput.UpdatedDate, 'MM-dd-yyyy');
if (latest_date > this.dateMinusTwoWeeks
&& this.systemInput.AMStatus == true
&& this.systemInput.BLStatus == true
&& this.systemInput.FirewallRullStatus == true
&& this.systemInput.FirewallContentStatus == true
&& this.systemInput.SCCMStatus == true
&& this.systemInput.MSBaselineStatus == true
&& this.systemInput.USBStatus == true
){return true }
else if(latest_date > this.dateMinusMonth && latest_date < this.dateMinusTwoWeeks
&& this.systemInput.AMStatus == true
&& this.systemInput.BLStatus == true
&& this.systemInput.FirewallRullStatus == true
&& this.systemInput.FirewallContentStatus == true
&& this.systemInput.SCCMStatus == true
&& this.systemInput.MSBaselineStatus == true
&& this.systemInput.USBStatus == true
){return 'datewarning'; }
else {return false}
}
I've tried to work this into the sort function using online resources, but I'm relatively new to all of this. Some help and a brief explanation would be much appreciated.
UPDATE:
My sort object is now sorting, but it's also updating the properties on my object. For example, it is randomly changing the values of b.USBstatus to false. I just want it to display this object first if all these flags are true. what am I doing wrong?
results.sort(function(a,b) {
if (b.AMStatus == true
&& b.BLStatus == true
&& b.FirewallRullStatus == true
&& b.FirewallContentStatus == true
&& b.SCCMStatus == true
&& b.MSBaselineStatus == true
&& b.USBStatus == true
){
return -1;
}
})));
}
Solution
Array.prototype.sort() takes compareFunction as parameter.
i.e. result.sort(sortObjects)
The logic behind compareFunction is that it should take two parameters, firstElement and secondElement, say a and b.
Then the compareFunction should look something like this,
function compare(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}
SOURCE - JavaScript Docs
Answered By - Vishnudev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.