Skip to content

Commit

Permalink
fix: Sort was buggy for keyword with >100 position
Browse files Browse the repository at this point in the history
resolves: #23
  • Loading branch information
towfiqi committed Dec 5, 2022
1 parent b450540 commit d22992b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions utils/sortFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
export const sortKeywords = (theKeywords:KeywordType[], sortBy:string) : KeywordType[] => {
let sortedItems = [];
const keywords = theKeywords.map((k) => ({ ...k, position: k.position === 0 ? 111 : k.position }));
switch (sortBy) {
case 'date_asc':
sortedItems = theKeywords.sort((a: KeywordType, b: KeywordType) => new Date(b.added).getTime() - new Date(a.added).getTime());
Expand All @@ -14,10 +15,12 @@ export const sortKeywords = (theKeywords:KeywordType[], sortBy:string) : Keyword
sortedItems = theKeywords.sort((a: KeywordType, b: KeywordType) => new Date(a.added).getTime() - new Date(b.added).getTime());
break;
case 'pos_asc':
sortedItems = theKeywords.sort((a: KeywordType, b: KeywordType) => (b.position > a.position ? 1 : -1));
sortedItems = keywords.sort((a: KeywordType, b: KeywordType) => (b.position > a.position ? 1 : -1));
sortedItems = sortedItems.map((k) => ({ ...k, position: k.position === 111 ? 0 : k.position }));
break;
case 'pos_desc':
sortedItems = theKeywords.sort((a: KeywordType, b: KeywordType) => (a.position > b.position ? 1 : -1));
sortedItems = keywords.sort((a: KeywordType, b: KeywordType) => (a.position > b.position ? 1 : -1));
sortedItems = sortedItems.map((k) => ({ ...k, position: k.position === 111 ? 0 : k.position }));
break;
case 'alpha_asc':
sortedItems = theKeywords.sort((a: KeywordType, b: KeywordType) => (b.keyword > a.keyword ? 1 : -1));
Expand Down

0 comments on commit d22992b

Please sign in to comment.