What is the best practice for me to sort an object of array by value of key that key is optional type from Object? I have following code that works if num is not optional type However, I got error that Object is possibly 'undefined'.
type Filter = {
a: number;
num?: number
}
const filter: Filter[] = [{a: 1, num: 10}, {a: 1, num: 12}, {a: 1, num: 5}]
filter.sort((a,b)=> (a.num > b.num ? 1 : -1))
console.log(filter)
[ {a:1}, {a:2}, {a:3, num: 10}]const filter: Filter[]instead ofconst filter: Filter?numshould be placed before those have a defined value."