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 } = React; const data = [ { name: 'John', age: 25, gender: 'Male' }, { name: 'Jane', age: 30, gender: 'Female' }, { name: 'Sam', age: 22, gender: 'Male' }, { name: 'Anna', age: 28, gender: 'Female' }, { name: 'Mike', age: 35, gender: 'Male' }, { name: 'Emily', age: 27, gender: 'Female' }, { name: 'Chris', age: 31, gender: 'Male' }, { name: 'Sarah', age: 29, gender: 'Female' }, { name: 'David', age: 26, gender: 'Male' }, { name: 'Olivia', age: 32, gender: 'Female' }, { name: 'Daniel', age: 24, gender: 'Male' }, { name: 'Sophia', age: 23, gender: 'Female' }, ]; function App() { const [sortedData, setSortedData] = useState(data); const [sortDirection, setSortDirection] = useState('asc'); const handleSort = () => { const newSortDirection = sortDirection === 'asc' ? 'desc' : 'asc'; const sorted = [...sortedData].sort((a, b) => { if (newSortDirection === 'asc') { return a.gender.localeCompare(b.gender); } else { return b.gender.localeCompare(a.gender); } }); setSortedData(sorted); setSortDirection(newSortDirection); }; return ( <div className='container'> <h1 className='title'>React Js Sort By Gender</h1> <button className='sort-button' onClick={handleSort}>Sort by Gender</button> <table className='data-table'> <thead> <tr> <th className='table-header'>Name</th> <th className='table-header'>Age</th> <th className='table-header'>Gender</th> </tr> </thead> <tbody> {sortedData.map((item, index) => ( <tr key={index} className='table-row'> <td className='table-data'>{item.name}</td> <td className='table-data'>{item.age}</td> <td className='table-data'>{item.gender}</td> </tr> ))} </tbody> </table> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> /* Container Styles */ .container { margin: 20px; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); background-color: #fff; } .title { font-size: 28px; margin-bottom: 20px; } /* Sort Button Styles */ .sort-button { background-color: #007bff; color: #fff; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; font-size: 18px; transition: background-color 0.3s ease; } .sort-button:hover { background-color: #0056b3; } /* Table Styles */ .data-table { width: 100%; border-collapse: collapse; margin-top: 20px; } .table-header { background-color: #007bff; color: #fff; padding: 10px; text-align: left; } .table-data { padding: 10px; border-bottom: 1px solid #ddd; } /* Table Row Styles */ .table-row:nth-child(even) { background-color: #f2f2f2; } .table-row:hover { background-color: #e0e0e0; } </style> </body> </html>