So I am trying to combine 4 arrays of different items and then sort those arrays by the column "created_at" as in newest first. So the resulting array should be an array of all those items sorted so that the newest item is the first no matter what type they are. I can sort it so that the newest item but it sorts through each type of item and then start sorting the other type.
Example: the types are shirts and pants. I upload a shirt at 10:00pm and then a pant at 10:15pm and then again a shirt at 10:30pm. The array should be like (shirt(10:30), pant(10:15), shirt(10:00)) but instead I get a (shirt(10:00), shirt(10:30), pant(10:15))
Here is my code:
@tops = Top.first(15)
@bottoms = Bottom.first(15)
@footwears = Footwear.first(15)
@accs = Accessory.first(15)
@tempItems = []
@temp = []
@temp = @tempItems + @tops
@temp = @temp + @bottoms
@temp = @temp + @footwears
@temp = @temp + @accs
@temp.sort_by{ |temp| - temp.created_at.to_i}
@itemss = @temp.first(15)
You need to assign the sorted array back to @temp
...
@temp = @temp.sort_by{ |temp| - temp.created_at.to_i}
You can reduce the confusion by getting rid of unnecessary assignment code:
fifteen= [Top, Bottom, FootWear, Accessory].
flat_map{ |c| c.first(15) }.
sort_by{ |e| -e.created_at.to_i }.
first(15)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.