<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
function App() {
const [data, setData] = useState([
{ id: 1, name: 'Mike' },
{ id: 2, name: 'John' },
{ id: 3, name: 'Anderson' },
{ id: 4, name: 'Sarah' },
{ id: 5, name: 'Emily' },
{ id: 6, name: 'David' },
{ id: 7, name: 'Sophia' },
{ id: 8, name: 'William' },
{ id: 9, name: 'Olivia' },
{ id: 10, name: 'James' },
{ id: 11, name: 'Emma' },
{ id: 12, name: 'Liam' },
{ id: 13, name: 'Ava' },
{ id: 14, name: 'Benjamin' },
{ id: 15, name: 'Mia' },
// Add more data as needed
]);
const [sortOrder, setSortOrder] = useState('asc');
const sortByColumn = () => {
const sortedData = [...data];
if (sortOrder === 'asc') {
sortedData.sort((a, b) => a.name.localeCompare(b.name));
setSortOrder('desc');
} else {
sortedData.sort((a, b) => b.name.localeCompare(a.name));
setSortOrder('asc');
}
setData(sortedData);
};
return (
<div className='container'>
<button className='sort-button' onClick={sortByColumn}>Sort by Name</button>
<table className='data-table'>
<thead>
<tr>
<th className='table-header'>ID</th>
<th className='table-header'>Name</th>
</tr>
</thead>
<tbody>
{data.map((item) => (
<tr key={item.id} className='table-row'>
<td className='table-cell'>{item.id}</td>
<td className='table-cell'>{item.name}</td>
</tr>
))}
</tbody>
</table>
</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;
}
/* Sort button styles */
.sort-button {
background-color: #3498db;
color: #fff;
border: none;
padding: 10px 20px;
margin-bottom: 20px;
cursor: pointer;
border-radius: 5px;
font-size: 16px;
}
.sort-button:hover {
background-color: #2980b9;
}
/* Table styles */
.data-table {
width: 100%;
border-collapse: collapse;
background-color: #fff;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
/* Table header styles */
.table-header {
background-color: #3498db;
color: #fff;
font-weight: bold;
padding: 10px;
text-align: left;
}
/* Table row styles */
.table-row:nth-child(even) {
background-color: #f2f2f2;
}
.table-row:hover {
background-color: #e0e0e0;
}
/* Table cell styles */
.table-cell {
padding: 10px;
border-bottom: 1px solid #ddd;
}
/* Remove border-bottom from the last cell in each row */
.table-row:last-child .table-cell {
border-bottom: none;
}
</style>
</body>
</html>