Skip to content

Commit

Permalink
Merge pull request #3 from TOY2-12/T1-10--feature/game/sorting
Browse files Browse the repository at this point in the history
Feat: rate 정렬
  • Loading branch information
Yamyam-code authored Nov 7, 2023
2 parents 5796289 + 3009696 commit a25748e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/components/game/rating/Rating.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
import sorting from './sorting';

const RatingWrapper = styled.div`
flex: 1;
Expand All @@ -9,19 +10,30 @@ const RatingWrapper = styled.div`
`;

export default function Rating() {
const [peoples, setPeoples] = useState(['']);
const [peoples, setPeoples] = useState([
{ id: 'dd', correct: 2 },
{ id: 'bb', correct: 2 },
{ id: 'cc', correct: 15 },
{ id: 'aa', correct: 15 },
]);
const [finish, setFinish] = useState(0);
useEffect(() => {
setPeoples(['aa', 'bb']);
}, []);
setPeoples(sorting(peoples));
setFinish(1);
}, [peoples]);

return (
<RatingWrapper>
<h1>Rating</h1>
{peoples.map((el, i) => (
<h2>
{i + 1}. {el}
</h2>
))}
{{ finish } ? (
peoples.map((el, i) => (
<h2>
{i + 1}. {el.id} X {el.correct}
</h2>
))
) : (
<div>X</div>
)}
</RatingWrapper>
);
}
8 changes: 8 additions & 0 deletions src/components/game/rating/sorting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Id {
id: string;
correct: number;
}

const sorting = (idArray: Id[]) => idArray.sort((a, b) => b.correct - a.correct || a.id.localeCompare(b.id));

export default sorting;

0 comments on commit a25748e

Please sign in to comment.