screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useEffect } = React; function App() { const initialData = [ { name: 'John', age: 25 }, { name: 'Alice', age: 30 }, { name: 'Bob', age: 28 }, { name: 'Eva', age: 32 }, { name: 'David', age: 27 }, { name: 'Sarah', age: 29 }, { name: 'Michael', age: 31 }, { name: 'Emma', age: 26 }, { name: 'Peter', age: 33 }, { name: 'Olivia', age: 24 }, ]; const [data, setData] = useState(initialData); const [sortOrder, setSortOrder] = useState('ascending'); const handleSort = () => { const sortedData = [...data].sort((a, b) => { if (sortOrder === 'ascending') { return a.name.localeCompare(b.name); } else { return b.name.localeCompare(a.name); } }); setData(sortedData); setSortOrder(sortOrder === 'ascending' ? 'descending' : 'ascending'); }; return ( <div className='container'> <h3>React Js Sort Array by Alphabet </h3> <button onClick={handleSort}>Sort {sortOrder === 'ascending' ? 'Descending' : 'Ascending'}</button> <ul> {data.map((item, index) => ( <li key={index}>{item.name} {item.age}</li> ))} </ul> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } .container { margin: 0 auto; width: 600px; text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); padding: 20px; } h1 { font-size: 24px; margin-bottom: 20px; color: #333; } button { background-color: #4caf50; color: white; border: none; padding: 10px 20px; font-size: 16px; border-radius: 5px; cursor: pointer; margin-bottom: 20px; transition: background-color 0.3s ease; } button:hover { background-color: #45a049; } ul { list-style: none; padding: 0; margin: 0; text-align: left; } li { padding: 10px 0; border-bottom: 1px solid #ccc; } li:last-child { border-bottom: none; } li:hover { background-color: #f0f0f0; } </style> </body> </html>