I have an angular 2 service apptService that returns Observable<Appointment[]> which I then order using the lodash sortBy function:
class Appointment {
id: string;
distance: number;
appointmentDate: date;
}
getAppointments(sortOrder) {
this.appointments = this
.apptService
.getAppointments()
.map(v => _.sortBy(v, sortOrder));
}
I call getAppointments passing a sort function like so:
getAppointments((app: Appointment) => app.id);
getAppointments((app: Appointment) => app.distance);
getAppointments((app: Appointment) => app.appointmentDate);
The appointmentDate and id sorts are as expected, but when sorting by distance, the values are ordered alphanumerically ie: 12, 123, 15, 3
For now, I have bodged an unsightly solution using:
getAppointments((app: Appointment) => {
// this will only work for appointments up to 100000 miles away
var s = "00000" + app.distance.toString();
return s.substr(s.length - 5);
});
but this breaks my pattern, so I am looking for a better solution, or hoping at least to understand why this is happening.
lodash was used for the sort because I liked the syntax and the ease of passing in the sorting arrow function, but am not attached to using it if there is a better solution.
And sorry but no plunker, I could not get lodash and typescript working together at all there.
getAppointments((app: Appointment) => +app.appointmentDate);parseInt(app.distance)asparseIntis only for strings. I couldparseInt(app.distance.toString())but then I think I'd be back where I started.