Skip to content

Commit

Permalink
feat: 도서 상태 정보를 청구기호 순으로 정렬 (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 authored Sep 20, 2023
1 parent 60a2d2e commit 1b5c241
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/component/book/BookDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import Image from "~/component/utils/Image";
import Like from "~/component/book/like/Like";
import TagWrapper from "~/component/book/tag/TagWrapper";
import "~/asset/css/BookDetail.css";
import { Book } from "~/type";

const callsignToNumbers = (callSign: string) =>
callSign
.replace(/[^0-9\.]/g, "")
.split(".")
.map(Number);

const compareCallsign = (a: Book, b: Book) => {
const xs = callsignToNumbers(a.callSign);
const ys = callsignToNumbers(b.callSign);

return xs.reduce((sum, x, i) => sum + (x - ys[i]), 0);
};

const BookDetail = () => {
const id = useParams().id || "";
Expand Down Expand Up @@ -104,9 +118,15 @@ const BookDetail = () => {
</span>
<div className="book-state__list">
<div>
{bookDetailInfo.books?.map((book, index) => (
<BookStatus key={book.id} book={book} index={index} />
))}
{bookDetailInfo.books
?.toSorted((a, b) => compareCallsign(a, b))
.map((book, index) => (
<BookStatus
key={book.callSign}
book={book}
index={index}
/>
))}
</div>
</div>
</div>
Expand Down

0 comments on commit 1b5c241

Please sign in to comment.