我正在构建一个拥有多个NPC的游戏。这些NPC包含在如下对象的数组中:
"[
{
"name": "Stella",
"gender": "Female",
"trait": "Strategic",
"traitid": 3,
"relationship": [
78,
86,
17,
64,
22,
65,
22,
15,
94,
16,
90,
34,
19,
90,
74,
33,
53,
46,
12,
93,
18,
97,
72,
57
],
"power": 100,
"startingnum": 10
},
{
"name": "Sue",
"gender": "Female",
"trait": "Strategic",
"traitid": 3,
"relationship": [
72,
39,
89,
48,
74,
26,
93,
37,
48,
17,
79,
39,
82,
56,
15,
14,
10,
52,
67,
100,
19,
21,
76,
29
],
"power": 88.599348534202,
"startingnum": 12
},
{
"name": "Adam",
"gender": "Male",
"trait": "Logistical",
"traitid": 0,
"relationship": [
60,
71,
33,
72,
31,
90,
20,
71,
28,
93,
45,
57,
98,
46,
85,
92,
33,
18,
19,
66,
77,
31,
14,
26
],
"power": 80.94462540716614,
"startingnum": 1
},
{
"name": "Lela",
"gender": "Female",
"trait": "Diplomatic",
"traitid": 2,
"relationship": [
75,
61,
54,
74,
81,
47,
55,
83,
30,
39,
73,
21,
58,
67,
93,
79,
46,
12,
100,
72,
66,
18,
73,
47
],
"power": 80.13029315960914,
"startingnum": 8
},
{
"name": "Fanny",
"gender": "Female",
"trait": "Diplomatic",
"traitid": 2,
"relationship": [
38,
61,
61,
29,
45,
77,
17,
56,
88,
44,
48,
65,
82,
49,
64,
79,
95,
61,
98,
41,
53,
73,
87,
39
],
"power": 76.38436482084691,
"startingnum": 18
},
{
"name": "Bernice",
"gender": "Female",
"trait": "Diplomatic",
"traitid": 2,
"relationship": [
99,
82,
60,
35,
59,
22,
80,
41,
26,
64,
76,
75,
47,
86,
19,
12,
38,
81,
88,
78,
23,
34,
12,
38
],
"power": 67.75244299674269,
"startingnum": 5
},
{
"name": "Charlotte",
"gender": "Female",
"trait": "Logistical",
"traitid": 0,
"relationship": [
99,
30,
90,
34,
24,
71,
15,
86,
61,
40,
79,
55,
86,
19,
90,
41,
91,
98,
22,
13,
14,
92,
91,
39
],
"power": 64.33224755700327,
"startingnum": 19
},
{
"name": "Myrtie",
"gender": "Female",
"trait": "Tactical",
"traitid": 1,
"relationship": [
24,
32,
26,
93,
46,
93,
70,
78,
92,
69,
46,
18,
83,
42,
67,
46,
86,
73,
83,
20,
43,
66,
66,
54
],
"power": 61.56351791530946,
"startingnum": 0
},
{
"name": "Lily",
"gender": "Female",
"trait": "Logistical",
"traitid": 0,
"relationship": [
57,
66,
95,
67,
36,
61,
95,
85,
38,
72,
84,
97,
45,
12,
88,
16,
32,
26,
84,
72,
50,
13,
97,
39
],
"power": 61.07491856677526,
"startingnum": 21
},
{
"name": "Willie",
"gender": "Male",
"trait": "Strategic",
"traitid": 3,
"relationship": [
50,
89,
26,
24,
76,
95,
71,
62,
79,
33,
37,
33,
93,
10,
16,
24,
26,
38,
41,
60,
40,
70,
85,
46
],
"power": 51.79153094462542,
"startingnum": 13
},
{
"name": "Madge",
"gender": "Female",
"trait": "Strategic",
"traitid": 3,
"relationship": [
14,
57,
35,
68,
65,
27,
72,
24,
86,
94,
64,
39,
73,
94,
26,
41,
10,
88,
23,
85,
56,
88,
45,
46
],
"power": 51.62866449511403,
"startingnum": 3
},
{
"name": "Dollie",
"gender": "Female",
"trait": "Strategic",
"traitid": 3,
"relationship": [
30,
81,
57,
22,
82,
54,
60,
14,
56,
45,
79,
33,
46,
89,
52,
56,
89,
78,
36,
63,
31,
31,
15,
36
],
"power": 49.67426710097722,
"startingnum": 2
},
{
"name": "Dennis",
"gender": "Male",
"trait": "Tactical",
"traitid": 1,
"relationship": [
68,
17,
39,
34,
87,
17,
20,
81,
74,
62,
99,
88,
25,
92,
33,
51,
17,
87,
66,
100,
99,
89,
12,
58
],
"power": 44.46254071661239,
"startingnum": 7
},
{
"name": "Inez",
"gender": "Female",
"trait": "Tactical",
"traitid": 1,
"relationship": [
80,
79,
56,
73,
25,
51,
29,
71,
74,
95,
69,
12,
54,
79,
72,
100,
72,
19,
66,
15,
95,
41,
14,
39
],
"power": 41.04234527687297,
"startingnum": 22
},
{
"name": "Harry",
"gender": "Male",
"trait": "Logistical",
"traitid": 0,
"relationship": [
72,
52,
69,
94,
10,
24,
77,
65,
56,
51,
17,
20,
31,
39,
24,
28,
85,
51,
34,
34,
64,
70,
67,
29
],
"power": 39.90228013029318,
"startingnum": 16
},
{
"name": "Ronald",
"gender": "Male",
"trait": "Logistical",
"traitid": 0,
"relationship": [
63,
99,
63,
68,
45,
58,
71,
24,
13,
20,
77,
83,
35,
36,
51,
74,
100,
46,
70,
74,
42,
55,
38,
41
],
"power": 37.133550488599354,
"startingnum": 9
},
{
"name": "Isabella",
"gender": "Female",
"trait": "Strategic",
"traitid": 3,
"relationship": [
55,
76,
72,
83,
31,
48,
94,
65,
61,
39,
31,
63,
94,
82,
38,
50,
37,
15,
24,
54,
24,
32,
92,
49
],
"power": 35.99348534201956,
"startingnum": 14
},
{
"name": "Steve",
"gender": "Male",
"trait": "Diplomatic",
"traitid": 2,
"relationship": [
85,
79,
47,
29,
28,
13,
14,
48,
75,
55,
71,
20,
76,
53,
13,
88,
91,
26,
79,
47,
50,
75,
70,
29
],
"power": 32.736156351791536,
"startingnum": 15
},
{
"name": "Clara",
"gender": "Female",
"trait": "Diplomatic",
"traitid": 2,
"relationship": [
26,
59,
92,
76,
27,
93,
15,
40,
97,
69,
78,
23,
77,
21,
18,
13,
12,
34,
57,
100,
32,
21,
13,
54
],
"power": 31.433224755700323,
"startingnum": 20
},
{
"name": "Wesley",
"gender": "Male",
"trait": "Logistical",
"traitid": 0,
"relationship": [
29,
88,
19,
50,
13,
89,
24,
11,
63,
19,
28,
82,
71,
59,
26,
63,
49,
66,
94,
36,
86,
18,
47,
36
],
"power": 27.035830618892504,
"startingnum": 11
},
{
"name": "Douglas",
"gender": "Male",
"trait": "Diplomatic",
"traitid": 2,
"relationship": [
19,
41,
20,
67,
14,
93,
31,
63,
60,
54,
85,
68,
89,
50,
65,
69,
77,
23,
86,
10,
38,
87,
16,
40
],
"power": 25.89576547231271,
"startingnum": 17
},
{
"name": "Fannie",
"gender": "Female",
"trait": "Tactical",
"traitid": 1,
"relationship": [
77,
19,
80,
13,
21,
42,
44,
96,
66,
40,
72,
47,
45,
69,
53,
52,
60,
64,
33,
95,
88,
79,
52,
42
],
"power": 24.429967426710103,
"startingnum": 6
},
{
"name": "Isaac",
"gender": "Male",
"trait": "Tactical",
"traitid": 1,
"relationship": [
37,
75,
63,
53,
89,
73,
17,
31,
34,
42,
96,
72,
70,
28,
49,
50,
25,
22,
100,
46,
18,
87,
67,
52
],
"power": 11.400651465798038,
"startingnum": 4
},
{
"name": "You",
"gender": "squid",
"trait": "none",
"traitid": -1,
"power": 0,
"startingnum": 24,
"relationship": [
73,
60,
44,
49,
41,
89,
46,
28,
95,
58,
93,
24,
67,
52,
92,
32,
13,
37,
89,
23,
69,
89,
33,
37
]
}
]"
等。我正在尝试按每个用户(对象)的功率级别对此数组进行排序。我正在使用下面的代码对其进行排序:
function SortByPower(obj1, obj2) {
// Ascending: first power more than the previous
return obj2.power - obj1.power;
};
users.sort(SortByPower);//sort the users by power
然而,它似乎只是改变了功率值的位置,或者其他一切。您可以在labs.questionable.co.nz/projects/raise查看正在进行的操作并查看其中的操作。你可以在日志中使用的东西:
答案 0 :(得分:0)
如果你可以使用lodash,它将是这样的(取自lodash文档)
_.sortBy([1, 2, 3], function(num) { return Math.sin(num); });
// → [3, 1, 2]
_.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
// → [3, 1, 2]
var characters = [
{ 'name': 'barney', 'age': 36 },
{ 'name': 'fred', 'age': 40 },
{ 'name': 'barney', 'age': 26 },
{ 'name': 'fred', 'age': 30 }
];
// using "_.pluck" callback shorthand
_.map(_.sortBy(characters, 'age'), _.values);
// → [['barney', 26], ['fred', 30], ['barney', 36], ['fred', 40]]
// sorting by multiple properties
_.map(_.sortBy(characters, ['name', 'age']), _.values);
// = > [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
您尝试做的一个很好的例子就是这里接受的答案:Sorting JavaScript Object by property value