screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/vue@3.2.12/dist/vue.global.js"></script> </head> <body> <div id="app"> <h3>Vue Js Sort Array Object by JSON by price property</h3> <button @click="sortItems">Sort by price</button> <ul> <li v-for="item in sortedItems" :key="item.id"> {{ item.name }} - {{ item.price }} </li> </ul> </div> <script type="module"> const app = Vue.createApp({ data() { return { items: [ { id: 1, name: 'Apple', price: 2.5 }, { id: 2, name: 'Orange', price: 1.8 }, { id: 3, name: 'Banana', price: 0.9 } ] }; }, methods: { sortItems() { this.items.sort((a, b) => a.price - b.price); } }, computed: { sortedItems() { return this.items; } } }); app.mount('#app'); </script> <style> #app { width: 600px; margin: 20px auto; box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .24); text-align: center; padding: 20px; } h3 { font-size: 20px; margin-bottom: 10px; } /* Styles for the button */ button { background-color: #4caf50; border: none; color: white; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; cursor: pointer; margin-bottom: 10px; border-radius: 4px; transition: background-color 0.3s ease; } button:hover { background-color: #45a049; } /* Styles for the list items */ ul { list-style-type: none; padding: 0; } li { margin-bottom: 5px; } </style> </body> </html>