I have Nuxt.js Hackernews project.
When I get posts from API of Hackernews I do not get sorted posts by time desc.
I need to sort posts by time desc (newer ones first).
/pages/index.vue
<template>
<div>
<button @click="reNew" class="btn"> Force update </button>
<div class="grid grid-cols-4 gap-5">
<div v-for="s in stories" :key="story">
<StoryCard :story="s"/>
</div>
</div>
</div>
</template>
<script>
definePageMeta({
layout: "stories"
})
export default {
data () {
return {
err: '',
stories: [],
interval:null,
}
},
methods: {
async reNew() {
await $fetch('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
.then((response) => {
let results = response.slice(0, 10)
results.forEach(id => {
$fetch('https://hacker-news.firebaseio.com/v0/item/'+ id +'.json?print=pretty')
.then((response) => {
this.stories.push(response)
})
.catch(err=> {
this.err = err
})
})
})
}
},
mounted() {
this.reNew()
},
created(){
this.interval = setInterval(() =>{
this.mounted()},5000)
},
destroyed(){
clearInterval(this.interval)
}
}
</script>
<style scoped>
.router-link-exact-active{
color:#12b488;
}
</style>
You can look at my code in
components/StoryCard.vue
below:
<template>
<div>
<div class="card text-center">
<h2><strong>Title: </strong>{{story.title}}</h2>
<p><strong>Score: </strong>{{story.score}}</p>
<p><strong>Author: </strong>{{story.by}}</p>
<p><strong>Date: </strong>{{ new Date(story.time*1000) }}</p>
<NuxtLink :to="`/${story.id}`">
<p class="btn my-4">View details</p> </NuxtLink>
</div>
</div>
</template>
<script setup>
const { story } = defineProps(['story'])
</script>
<style scoped>
.thumb {
max-height: 120px;
max-width: 70%;
margin: 0 auto;
}
</style>
How can I sort posts by time desc (newer ones first)?
How can I do that here using Javascript?