Data Table with Sort/Filter in JavaScript
Filter a marks table in JavaScript with includes and sort by numeric subtraction. Render new rows from a filtered array without reordering the source list.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<label>Find student <input id="filter" type="search"></label><button id="sort">Sort by marks: low to high</button><table><caption>Class test marks</caption><thead><tr><th>Name</th><th>Marks</th></tr></thead><tbody id="rows"></tbody></table>7<script>8const students = [{ name: 'Ravi', marks: 72 }, { name: 'Asha', marks: 91 }, { name: 'Noor', marks: 84 }];9let ascending = true;10function render() {11 const query = document.querySelector('#filter').value.trim().toLowerCase();12 const visible = students.filter(student => student.name.toLowerCase().includes(query));13 visible.sort((a, b) => ascending ? a.marks - b.marks : b.marks - a.marks);14 const rows = document.querySelector('#rows'); rows.replaceChildren();15 for (const student of visible) {16 const row = document.createElement('tr');17 for (const value of [student.name, student.marks]) { const cell = document.createElement('td'); cell.textContent = value; row.append(cell); }18 rows.append(row);19 }20}21document.querySelector('#filter').oninput = render;22document.querySelector('#sort').onclick = () => {23 ascending = !ascending;24 document.querySelector('#sort').textContent = 'Sort by marks: ' + (ascending ? 'low to high' : 'high to low'); render();25};26render();27</script>28</body>29</html>
Sample Output
Ravi 72, Noor 84, Asha 91
The initial view sorts marks in ascending numeric order.
Asha 91 after filtering as
Only Asha contains the normalized query. Other records remain in the original array.
In this one
- Filter a table by normalized text
- Sort numbers with a comparator
- Keep source data separate from visible rows
- Render actual table cells
How it works
- 1
Read the filter query and convert it to lowercase.
- 2
Build a filtered copy of the students array.
- 3
Sort that copy by numeric marks in the chosen direction.
- 4
Replace the table body with rows for the current view.
- 5
Switch sorting, then type as to leave only Asha with 91 marks.
Edge cases
Numeric values stored as strings
Default sort can order 100 before 20.
Use numeric marks and an explicit subtraction comparator. Validate numbers when reading external data.
No matching name
The filtered view can contain no rows.
This is not a data deletion. Clear the query to show the original students again.
What's going on
A data table puts related values into rows and columns. This class-marks table adds a name filter and a button that reverses numeric sorting. It is a useful exercise because the visible rows are a view of the data, not the only copy of it.
The filter normalizes the query and student names to lowercase. filter returns a new array, so sorting that array does not reorder the original students list. The comparator subtracts marks to sort numerically. A comparator is a function that tells sort which of two items should come first.
render creates real table rows with cells and displays values as text. An empty query shows all students; a query with no matches leaves an empty table body. The button text describes the currently displayed direction after each click. This small example assumes all marks are valid numbers. A larger table should show a clear no-results message and support the needed screen-reader sorting information.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which method creates the view copy here?
Pick an answer to check it.
Question 2
What does a.marks - b.marks produce for ascending order?
Pick an answer to check it.
Questions people ask
Does filtering delete students?
No. It creates a new view array. The original list remains unchanged.
Why not call sort with no function?
The default compares string forms. Subtraction makes the intended numeric order explicit.
How would I sort names?
Use a name comparator such as a.name.localeCompare(b.name), rather than subtracting strings.