From 315505eda225e7c1c897f7361b8b94dc76b432da Mon Sep 17 00:00:00 2001 From: Wooyeol Lee Date: Thu, 7 Dec 2023 15:18:15 +0900 Subject: [PATCH] fix: sort saved emojis by descending order of saved time --- .../domain/emoji/service/EmojiService.kt | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt index 02fabaea..41792e45 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt @@ -51,21 +51,18 @@ class EmojiService( } else { user.saved_emojis } - var emojiList = mutableListOf() + val emojiArrayDeque = ArrayDeque() + var emojiList = listOf() if (emojiIdList != null && emojiIdList.size != 0) { for (emojiId in emojiIdList) { val emoji = emojiDao.getEmoji(emojiId) ?: continue - emojiList.add(emoji) - } - // sort - if (emojiList.size != 0) { - emojiList.sortByDescending { it.created_at } - // pagination - emojiList = emojiList.subList( - min((index - 1) * count, emojiList.size - 1), - min(index * count, emojiList.size) - ) + emojiArrayDeque.addFirst(emoji) } + // pagination + // FIXME: remind - CreatedEmojiList should already be sorted by created_at + val start = min((index - 1) * count, emojiArrayDeque.size - 1) + val end = min(index * count, emojiArrayDeque.size) + emojiList = emojiArrayDeque.toList().subList(start, end) } return emojiList }