Here is a snippet of code I'm working on in TypeScript:
public static sortByProperty<T>( array: T[], prop: string ): void
{
var availProps: string[] = Object.getOwnPropertyNames( T ); // or something typeof T, anyway I got error
if(availProps.length>0 && availProps.indexOf(prop) > -1){
return array.Sort(function(a, b) {
var aItem = a[prop];
var bItem = b[prop];
return ((aItem < bItem ) ? -1 : ((aItem > bItem ) ? 1 : 0));
});
}
}
In my code, I want to utilize it like this:
Test.sortByProperty<MyObject>(arrayOf_MyObject, "APropertyName");
However, I encountered an error stating that T
is unknown.