-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortbyname.ts
47 lines (37 loc) · 1007 Bytes
/
sortbyname.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
interface INamed {
name: string;
}
function sortByName<T extends INamed>(a: T[]) {
var result = a.slice(0);
result.sort((x, y) => x.name.localeCompare(y.name));
return result;
}
sortByName([]);
var arr = [{ name: "Daniele", age: 42 }, { name: "Pippo", age: 18 }, { name: "Arcibald", age: 90 }];
var ret = sortByName(arr);
ret[0].name;
ret[1].age;
var arr2 = [{ name: "Daniele", sex: "M" }, { name: "Pippo", sex: "M" }, { name: "Arcibalda", sex: "F" }];
var ret2 = sortByName(arr2);
ret2[1].name;
ret2[0].sex;
console.log(ret);
class PName {
name: string;
constructor(name: string) {
this.name = name;
}
}
class PNameAge extends PName {
constructor(name: string, public age: number) {
super(name);
}
}
var arr3 = [{ name: "Daniele", sex: "M" }, new PName("pippo"), new PNameAge("Pluto", 42)];
var ret3 = sortByName(arr3);
ret3[1].name;
ret3[0].sex;
var pluto = ret3.find(e => e.name == "Pluto");
if (pluto instanceof PNameAge) {
pluto.age;
}