ga-wdi-exercises / project4

[project]
https://github.com/ga-wdi-exercises/project4
2 stars 8 forks source link

reverse seed data/entries #535

Closed daniellefricke closed 7 years ago

daniellefricke commented 7 years ago

Hi! I'm trying to reverse the order of my seed data so that the most recent entries appear at the top when users create a submission. I think I want to use the .reverse() method but am not sure where to put it. Would it go in my server.js file or would it go in the individual component? I've tried a few places but am not getting it to work.

Thanks!

superbuggy commented 7 years ago

Try this, instead of just using .find({}), try .find({}).sort('-date')

daniellefricke commented 7 years ago

Would the value of the date key need to be in a particular format? I currently just have it displaying date as a string i.e. Spring 2017, Fall 2015, and it doesn't work. I could change it to a format if needed. Thoughts?

superbuggy commented 7 years ago

Ah, I see, in that case you'd have to write a custom filter.

First step will be extracting the date year from the string. You can use a method .match for this along with an regular expression to grab an entire number.

"Spring 2016".match(/\d+/) // returns a match object
"Spring 2016".match(/\d+/)[0] // returns the number contained in the String

Second step: you will have to sort according to Season. Using an object to determine the order can be useful.

const seasonsOrder = {
  Spring: 0,
  Summer: 1,
  Fall: 2,
  Winter: 3
}

This object's purpose is to convert a season into an integer value that can be used to sort within years (.sort() would be appropriate for this).