-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew 2.js
60 lines (54 loc) · 994 Bytes
/
new 2.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
var students = [{name:"Cindy",age:"100",score:"88"},{name:"Aindy",age:"100",score:"88"},{name:"Dindy",age:"99",score:"88"}];
function createCompact(pNum){
return function (object1,object2)
{
var value1 = parseInt(object1[pNum]);
var value2 = parseInt(object2[pNum]);
if(value1 < value2)
{
return -1;
}
else if(value1 > value2)
{
return 1;
}
else
{
return 0;
}
};
}
function createCompactName(pName){
return function (object1,object2)
{
var value1 = object1[pName];
var value2 = object2[pName];
if(value1 < value2)
{
return -1;
}
else if(value1 > value2)
{
return 1;
}
else
{
return 0;
}
};
}
students.sort(createCompactName('name'));
students.sort(createCompact('score'));
students.sort(createCompact('age'));
cnt = 1;
students[0].index = cnt;
for(var i = 1;i < students.length;i++){
if(students[i].age == students[i-1].age){
cnt++;
students[i].index = cnt;
}
else{
cnt = 1;
students[i].index = cnt;
}
};