File tree Expand file tree Collapse file tree 2 files changed +11
-8
lines changed
data-access-documents/src/lib Expand file tree Collapse file tree 2 files changed +11
-8
lines changed Original file line number Diff line number Diff line change @@ -2,8 +2,8 @@ import { BlogPostDataEntry } from './blog.model';
22
33export function sortPosts ( posts : BlogPostDataEntry [ ] ) : BlogPostDataEntry [ ] {
44 return posts . sort ( ( a , b ) => {
5- if ( a . pinned && ! b . pinned ) return - 1 ;
6- if ( b . pinned && ! a . pinned ) return 1 ;
5+ if ( a . pinned === true && b . pinned !== true ) return - 1 ;
6+ if ( b . pinned === true && a . pinned !== true ) return 1 ;
77 return new Date ( b . date ) . getTime ( ) - new Date ( a . date ) . getTime ( ) ;
88 } ) ;
99}
Original file line number Diff line number Diff line change @@ -30,14 +30,17 @@ export interface BlogContainerProps {
3030export function sortFirstFivePosts (
3131 posts : BlogPostDataEntry [ ]
3232) : BlogPostDataEntry [ ] {
33- // Separate posts: pinned-able posts first
34- const allowedPinnedPosts = posts . filter ( ( p ) => p . pinned !== false ) ;
33+ // Sort posts: pinned posts first, then by date
34+ const sortedPosts = posts . sort ( ( a , b ) => {
35+ // If one is pinned and the other isn't, prioritize the pinned one
36+ if ( a . pinned === true && b . pinned !== true ) return - 1 ;
37+ if ( b . pinned === true && a . pinned !== true ) return 1 ;
3538
36- allowedPinnedPosts . sort (
37- ( a , b ) => new Date ( b . date ) . valueOf ( ) - new Date ( a . date ) . valueOf ( )
38- ) ;
39+ // Otherwise, sort by date (newest first)
40+ return new Date ( b . date ) . valueOf ( ) - new Date ( a . date ) . valueOf ( ) ;
41+ } ) ;
3942
40- return allowedPinnedPosts . slice ( 0 , 5 ) ;
43+ return sortedPosts . slice ( 0 , 5 ) ;
4144}
4245
4346export function BlogContainer ( { blogPosts, tags } : BlogContainerProps ) {
You can’t perform that action at this time.
0 commit comments