繁体   English   中英

JavaScript 作业问题中的自定义排序

[英]Custom sorting in JavaScript homework question

编写一个名为sort_by_average_rating的函数,它将键值存储列表/数组作为参数,其中每个键值存储都有键ratingsbudgetbox_office ,其中budgetbox_office是整数, ratings 是整数列表。 根据ratings值的平均值对输入进行排序。

当键等于["ratings"]时,我尝试在 for 循环中添加所有内容,然后我对两个 for 循环的结果求平均值。 最后,我使用了一个单独的函数来对所有内容进行排序。

function key(a,b){
  for(var i = 0; i < a.length; i++){
    tol1 += a[i]["ratings"];  

  for(var x = 0; x < b.length; x++) {
    tol2 += b[x]["ratings"];
    }
    var average = (tol1/tol2);
  }
  return average;
}

function key(x,y){
  if (x[key] < x[key]) { return -1; }
  if (y[key] > y[key]) { return 1; }
  return 0;
 }

function sort_by_average_rating(b){
  b.sort(key)
 }

结果:

[
    {'box_office': 12574015, 'budget': 3986053.18, 'ratings': [8, 7, 1]}, 
    {'box_office': 44855251, 'budget': 3301717.62, 'ratings': [7, 1, 1]}, 
    {'box_office': 36625133, 'budget': 8678591, 'ratings': [7, 6, 2, 8]}, 
    {'box_office': 48397691, 'budget': 15916122.88, 'ratings': [7, 3, 8, 8, 6, 8]}, 
    {'box_office': 43344800, 'budget': 4373679.25, 'ratings': [1, 1, 7, 4]}
]

预期的:

[
    {'box_office': 44855251, 'budget': 3301717.62, 'ratings': [7, 1, 1]}, 
    {'box_office': 43344800, 'budget': 4373679.25, 'ratings': [1, 1, 7, 4]}, 
    {'box_office': 12574015, 'budget': 3986053.18, 'ratings': [8, 7, 1]}, 
    {'box_office': 36625133, 'budget': 8678591.0, 'ratings': [7, 6, 2, 8]}, 
    {'box_office': 48397691, 'budget': 15916122.88, 'ratings': [7, 3, 8, 8, 6, 8]}
]

一种可能的解决方案是创建一个返回ratings数组average的函数,我们将为此使用Array.reduce() 然后你可以使用这个带有内置Array.sort() 的新函数来生成你想要的结果:

 const input = [ {'box_office': 12574015, 'budget': 3986053.18, 'ratings': [8, 7, 1]}, {'box_office': 44855251, 'budget': 3301717.62, 'ratings': [7, 1, 1]}, {'box_office': 36625133, 'budget': 8678591, 'ratings': [7, 6, 2, 8]}, {'box_office': 48397691, 'budget': 15916122.88, 'ratings': [7, 3, 8, 8, 6, 8]}, {'box_office': 43344800, 'budget': 4373679.25, 'ratings': [1, 1, 7, 4]} ]; const getAverage = (arr) => { return (Array.isArray(arr) && arr.length > 0) ? arr.reduce((acc, n) => acc + n) / arr.length : 0; }; input.sort((a, b) => getAverage(a.ratings) - getAverage(b.ratings)); console.log(input);
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}

为避免原始数据发生突变,请使用:

let sorted = input.slice().sort((a, b) => getAverage(a.ratings) - getAverage(b.ratings));

我建议使用不同的方法( 使用 map 排序),通过获取数组中的平均值和另一个数组作为索引,对索引数组进行排序并通过获取索引来映射对象。

 const add = (a, b) => a + b; var array = [{ box_office: 12574015, budget: 3986053.18, ratings: [8, 7, 1] }, { box_office: 44855251, budget: 3301717.62, ratings: [7, 1, 1] }, { box_office: 36625133, budget: 8678591, ratings: [7, 6, 2, 8] }, { box_office: 48397691, budget: 15916122.88, ratings: [7, 3, 8, 8, 6, 8] }, { box_office: 43344800, budget: 4373679.25, ratings: [1, 1, 7, 4] }], averages = array.map(({ ratings }) => ratings.reduce(add, 0) / ratings.length), indices = [...averages.keys()].sort((i, j) => averages[i] - averages[j]), result = indices.map(i => array[i]); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

1) 你应该在使用它们之前初始化你的变量(例如tol1

2)你应该避免重复函数名称(例如key

3)你应该给你的变量和函数命名一些有意义的东西(例如'a','b','key','x','y')没有任何意义。

 let movies = [ {'box_office': 12574015, 'budget': 3986053.18, 'ratings': [8, 7, 1]}, {'box_office': 44855251, 'budget': 3301717.62, 'ratings': [7, 1, 1]}, {'box_office': 36625133, 'budget': 8678591, 'ratings': [7, 6, 2, 8]}, {'box_office': 48397691, 'budget': 15916122.88, 'ratings': [7, 3, 8, 8, 6, 8]}, {'box_office': 43344800, 'budget': 4373679.25, 'ratings': [1, 1, 7, 4]} ]; let averageRating = movie => movie.ratings.reduce((rating, sum) => rating + sum, 0) / movie.ratings.length; let sortedMovies = movies.sort((movie1, movie2) => averageRating(movie1) - averageRating(movie2)); console.log(sortedMovies);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM