To enhance the functionality, I propose creating two auxiliary functions to compare two Point
s and two arrays of AnotherPoint
s separately. The comparison would be based on element-by-element analysis, halting once a pair of elements fails to match with each other.
function comparePoints(pA: Point | AnotherPoint, pB: Point | AnotherPoint)
{
const pointA = pA.Name.toLowerCase();
const pointB = pB.Name.toLowerCase();
if (pointA < pointB ) {
return -1;
}
if (pointA > pointB ) {
return 1;
}
const timeA = pA.Timestamp;
const timeB = pB.Timestamp;
return Service.compareDate(new Date(timeA), new Date(timeB));
}
function comparePointArrays(arrayA: AnotherPoint[], arrayB: AnotherPoint[])
{
const len = Math.min(arrayA.length, arrayB.length);
for (let i = 0; i < len; ++i)
{
const result = comparePoints(arrayA[i], arrayB[i]);
if (result !== 0) {
return result;
}
}
return 0;
}
Subsequently, the sorting procedure can be refined by incorporating these new comparator functions:
private sortByNameAndID(dataModel: DataModel[]): DataModel[] {
return dataModel.sort(function (a, b) {
return comparePoints(a.point1, b.point1) ||
comparePoints(a.point2, b.point2) ||
comparePoints(a.point3, b.point3) ||
comparePointArrays(a.AnotherPoint1, b.AnotherPoint1) ||
comparePointArrays(a.AnotherPoint2, b.AnotherPoint2) ||
comparePointArrays(a.AnotherPoint3, b.AnotherPoint3);
});
}
It is essential to note that the ||
operator executes the operation on the right only if the preceding operation yields a falsy (0) outcome, implying that comparisons cease as soon as there is a non-zero result.