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

时间:2019-04-23 18:15:29

标签: javascript arrays sorting

编写一个名为from bs4 import BeautifulSoup as Wsoup from difflib import SequenceMatcher def similar(a, b): return SequenceMatcher(None, a, b).ratio() x = "The Eggplant the Witch and the Wardrobe 720p AMZN WEB-DL DD+5 1 H 264-QOQ" scn_rls_soup = Wsoup(my_driver, "html.parser") found = scn_rls_soup.findAll(text=True) for text in found: if similar(x,text) > 0.8: print(text) 的函数,该函数将键值存储的列表/数组作为参数,其中每个键值存储都有键sort_by_average_ratingratings和{{1 }},其中budgetbox_office是整数,而rating是整数列表。根据{{​​1}}中值的平均值对输入进行排序。

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

box_office

结果:

ratings

预期:

["ratings"]

3 个答案:

答案 0 :(得分:3)

一个可能的解决方案是创建一个返回average数组的ratings的函数,我们将为此使用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));

答案 1 :(得分:3)

我建议使用不同的方法(sorting with 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; }

答案 2 :(得分:2)

1)您应该在使用变量之前对其进行初始化(例如tol1

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

3)您应该为变量和函数命名有意义的名称(例如“ a”,“ b”,“键”,“ 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);