<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>
<script type="text/babel">
const { useState } = React
{ id: 2, product: "Apple Iphone" },
{ id: 5, product: "Vivo Mobile" },
{ id: 3, product: "Samsung Tablet" },
{ id: 7, product: "Google Pixel" },
{ id: 11, product: "Sony Xperia" },
{ id: 17, product: "Huawei Laptop" },
{ id: 10, product: "Dell XPS" },
{ id: 34, product: "Lenovo ThinkPad" },
{ id: 22, product: "HP Printer" },
{ id: 43, product: "Canon Camera" },
{ id: 89, product: "Bose Headphones" },
{ id: 21, product: "LG Smart TV" },
{ id: 13, product: "Microsoft Surface" }
const [data, setData] = useState(initialData);
const [sortOrder, setSortOrder] = useState("asc");
// Function to handle sorting by ID
const sortedData = [...data].sort((a, b) => {
if (sortOrder === "asc") {
setSortOrder(sortOrder === "asc" ? "desc" : "asc");
<div className='container'>
<h3 className='title'>Rect Js Table Sort by id</h3>
<button className='sort-button' onClick={sortById}>Sort by ID</button>
<table className='data-table'>
<th className='table-header'>ID</th>
<th className='table-header'>Product</th>
<tr key={item.id} className='table-row'>
<td className='table-cell'>{item.id}</td>
<td className='table-cell'>{item.product}</td>
ReactDOM.render(<App />, document.getElementById("app"));
/* Styles for the container */
font-family: Roboto, Helvetica, Arial, sans-serif;
color: rgba(0, 0, 0, 0.87);
background-color: rgb(255, 255, 255);
box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 1px -1px, rgba(0, 0, 0, 0.14) 0px 1px 1px 0px, rgba(0, 0, 0, 0.12) 0px 1px 3px 0px;
/* Styles for the title */
/* Styles for the sort button */
background-color: #007bff;
background-color: #0056b3;
/* Styles for the data table */
border-collapse: collapse;
/* Styles for table headers */
/* Styles for table rows */
.table-row:nth-child(even) {
background-color: #f2f2f2;
background-color: #e0e0e0;
/* Styles for table cells */