[英]Sort user repositories by Stars Count in Github API
I'm building a reactjs app to show a specific user repositories. 我正在构建一个reactjs应用程序以显示特定的用户存储库。 I need to sort them by star count but i dont know how to do it in React.
我需要按星数对它们进行排序,但我不知道如何在React中进行。
This is the api url which gives me stars number: https://api.github.com/users/cesar/repos 这是给我星星编号的api网址: https : //api.github.com/users/cesar/repos
This is my actual code: 这是我的实际代码:
const UserRepositories = props => (
<div className="user-repos">
<ul>
<li>
<h2 className="repo-name">{props.repoName}</h2>
</li>
<li>
<p className="repo-description">{props.repoDescription}</p>
</li>
<li>
<span>
<FontAwesomeIcon icon={Star} className="icon" />
<span className="star-number">{props.starNumber}</span>
</span>
</li>
</ul>
</div>
);
And this is my map where i print the user repositories 这是我在其中打印用户存储库的地图
<div className="col-md-8">
{githubRepo.map(name => (
<UserRepositories
key={name.id}
repoName={name.name}
repoDescription={name.description}
starNumber={name.stargazers_count}
/>
))}
</div>
You can sort your repositories using the sort
function before calling the map
function. 您可以在调用
map
函数之前使用sort
函数对存储库进行sort
。
<div className="col-md-8">
{
githubRepo.sort((a, b) => {
if (a.stargazers_count > b.stargazers_count) return 1
else if (a.stargazers_count < b.stargazers_count) return -1
return 0
}).map(name => (
<UserRepositories
key={name.id}
repoName={name.name}
repoDescription={name.description}
starNumber={name.stargazers_count}
/>
))
}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.