I'm trying to display a list of objects on screen with custom order [m,r,d,v,o]. I've tried using the sort() function which works for the first and last items but the middle comes through randomly.
Should I be using the sort() function for this situation? If so what am I doing wrong?
In the UI I'm using a *ngFor to loop through array.
<div *ngFor="let stuff of sortedStuff; let i = index;">
<div>want to display in the custom sort order here</div>
<ul><li> {{ stuff.someproperty }} <li></ul>
</div>
Code that builds the array:
var sortedStuff = stuff.splice(0);
sortedStuff.sort((obj1) => {
if (obj1.propertyX === 'M') {
return -1;
}
if (obj1.propertyX!= 'M' && obj1.propertyX!= 'D' && obj1.productType != 'V' && obj1.propertyX!= 'O' && obj1.propertyX=== 'R'){
return 1;
}
if (obj1.propertyX!= 'M' && obj1.propertyX!= 'R' && obj1.propertyX!= 'V' && obj1.propertyX!= 'O' && obj1.propertyX === 'D'){
return 1;
}
if (obj1.propertyX!= 'M' && obj1.propertyX!= 'D' && obj1.propertyX!= 'R' && obj1.propertyX!= 'O' && obj1.propertyX === 'V'){
return 1;
}
if (obj1.obj1.propertyX != 'M' && obj1.obj1.propertyX != 'D' && obj1.obj1.propertyX != 'V' && obj1.obj1.propertyX != 'R' && obj1.obj1.propertyX === 'O'){
return 1;
}
return 0;
});