1

I tried to sort the date using computed properties but it didnt work, if i using at methods and remove the slice, then it will sort as expected. I've tried to split the date also but still not working. I am not sure what caused the problem.

Kindly find my code as below.

App

<template>
    <div>
        <h2>Ingredients</h2>
        <ul>
            <li></li>
        </ul>
        <ingredients-list  v-for="(ingredient,index) in ingredients"
            :key="index"
            :index='index'
            :foodName="ingredient.food"
            :foodExpiry="ingredient.expiryDate">
        </ingredients-list>
    </div>
</template>
    
<script>
    export default {
        data(){
            return{
                ingredients:[
                {
                    food:'carrot',
                    expiryDate:'2020-12-12'
                },
                {
                    food:'papaya',
                    expiryDate:'2018-1-15'
                },
                {
                    food:'orange',
                    expiryDate:'2021-10-13'
                },
                {
                    food:'meat',
                    expiryDate:'2019-4-23'
                }]
            }
        },
        computed: {
            sortedItems() {
                return this.ingredients.slice().sort( ( a, b) => {
                    return new Date(a.expiryDate)- new Date(b.expiryDate);
                });
            }
        }
    }
</script>

components

<template>
    <div>
        <h2>{{index}}</h2>
        <h2>Food:{{foodName}}</h2>
        <h2>Expiry:{{foodExpiry}}</h2>
    </div>
</template>
    
<script>
    export default {
        props:['foodName','foodExpiry'],
    }
</script>
3
  • You are not using the sortedItems property. Commented Dec 28, 2020 at 19:44
  • Hi Abdelillah, i want it to be autosorted whenever the page update Commented Dec 28, 2020 at 19:58
  • You mean every time a component created and mounted? Commented Dec 28, 2020 at 20:10

2 Answers 2

2

Like @Anatoly said

Computed props are never calculated if they are not used at all.

What you should be doing to solve the problem is :

  1. Using slice method instead of splice since it mutates the original array:
sortedItems() {
      return this.ingredients.slice()
             .sort((a, b) => new Date(a.expiryDate)- new Date(b.expiryDate));
}
  1. Loop through the computed property and not the original array:
<ingredients-list  v-for="(ingredient,index) in sortedItems"
            :key="index"
            :index='index'
            :foodName="ingredient.food"
            :foodExpiry="ingredient.expiryDate">
</ingredients-list>
Sign up to request clarification or add additional context in comments.

Comments

1

It seems you confused splice with slice. You need slice to get a copy of an array and to sort this copy:

 sortedItems() {
            return this.ingredients.slice().sort( ( a, b) => {
                
                return new Date(a.expiryDate)- new Date(b.expiryDate);
            });
        }

2 Comments

Hi, I've tried with slice but still unable to sort
Computed props are never calculated if they are not used at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.