GetSortOrder("weeklyPercentageChange[0]")
приведет к поиску буквального имени свойства "weeklyPercentageChange[0]"
, то есть, если объекты были определены примерно как
{
user: 'name',
'weeklyPercentageChange[0]': 55
}
Это не то, что у вас есть, поэтому все работает не так, как ожидалось.
Для общего решения навигации по вложенному объекту или массиву для поиска значения для сортировки рассмотрите возможность передачи обратного вызова вместо него, чтобы перейти к вложенному значению, с которым вы хотите сравнить ; здесь, obj => obj.weeklyPercentageChange[0]
.
Вы также можете сделать код значительно более кратким, просто возвращая разницу между значениями:
var array=[{user:"Noah",weeklyPercentageChange:["10.37","-3.75","-2.21","-1.85","-1.68","-1.21","-1.41","-1.17","-0.47","-1.64","-23.36"]},{user:"Liam",weeklyPercentageChange:["-4.49","-2.61","-1.19","-0.36","-1.39","-1.29","-0.44","-0.22","-0.67","-0.33","-12.53"]},{user:"Mason",weeklyPercentageChange:["3.22","-2.40","0.35","-1.80","-0.25","-0.66","-2.42","0.00","1.71","-0.52","-9.00"]},{user:"Ethan",weeklyPercentageChange:["-1.31","0.00","-1.75","-0.78","0.61","-1.79","0.80","0.00","0.00","0.38","-3.75"]},{user:"Alexander",weeklyPercentageChange:["-1.26","0.00","-1.48","-1.50","-0.07","-1.66","-0.53","-0.20","0.00","-0.86","-7.42"]},{user:"Maria",weeklyPercentageChange:["-1.10","-1.90","-0.11","-0.63","2.52","-1.01","0.06","-0.06","-0.06","1.38","-0.94"]},{user:"Paul",weeklyPercentageChange:["1.08","-2.33","-0.10","-1.17","1.83","-1.70","-1.13","-0.19","0.00","0.00","-5.84"]},{user:"Steven",weeklyPercentageChange:["-0.77","-2.93","0.19","-1.22","-0.48","-1.39","-1.27","0.00","-0.23","-0.91","-8.84"]},{user:"Edward",weeklyPercentageChange:["-0.71","-1.73","1.46","-0.17","0.42","-1.23","-1.26","1.47","0.59","-2.81","-3.98"]},{user:"Benjamin",weeklyPercentageChange:["0.56","-0.28","0.80","-0.94","0.00","0.90","-0.05","1.17","-1.87","0.98","0.14"]},{user:"Zachary",weeklyPercentageChange:["-0.47","-1.29","0.48","-1.59","1.37","-2.65","-0.24","0.66","0.05","0.24","-3.41"]},{user:"Austin",weeklyPercentageChange:["-0.33","0.00","-3.05","-2.02","0.80","-1.48","1.58","-0.43","0.87","-1.90","-5.87"]},{user:"Jordan",weeklyPercentageChange:["0.06","-2.18","2.17","-0.56","0.84","-0.67","0.45","0.39","0.28","0.06","0.67"]},{user:"Gabriel",weeklyPercentageChange:["0.00","0.00","-0.12","-0.81","0.25","0.00","0.00","0.00","0.00","0.00","-0.68"]},{user:"Wayne",weeklyPercentageChange:["0.20","-3.45","0.35","0.49","0.56","0.00","-0.61","0.54","0.54","-0.41","-1.83"]},{user:"Willie",weeklyPercentageChange:["-0.83","-3.21","0.71","0.00","-1.05","0.35","0.07","0.14","-0.07","-1.45","-3.72"]},{user:"Frank",weeklyPercentageChange:["0.00","0.00","-3.04","0.88","1.37","-0.41","-1.21","2.10","0.00","-0.58","-0.94"]}];
const GetSortOrder = getProp => (a,b) => getProp(a) - getProp(b);
array.sort( GetSortOrder(obj => obj.weeklyPercentageChange[0]) );
document.write("<br><br> Sorted Weekly Percentage Change : <br>");
for (var item in array) {
document.write("<br>"+array[item].weeklyPercentageChange[0]);
}